vendredi 20 septembre 2019

Why does pytest coverage skip custom exception message assertion?

I'm making a wrapper for an api. I wanted the function to return a custom exception message upon invalid input.

def scrap(date, x, y):
    response = requests.post(api_url, json={'date': date, 'x': x, 'y': y})
    if response.status_code == 200:
        output = loads(response.content.decode('utf-8'))
        return output
    else:
        raise Exception('Invalid input')

This is the test for it:

from scrap import scrap

def test_scrap():
    with pytest.raises(Exception) as e:
        assert scrap(date='test', x=0, y=0)
        assert str(e.value) == 'Invalid input'

But the coverage tests skips the last line for some reason. Does anyone know why? I tried changing the code to with pytest.raises(Exception, match = 'Invalid input') as e, but i get an error:

AssertionError: Pattern 'Invalid input' not found in "date data 'test' does not match format '%Y-%m-%d %H:%M:%S'"

Does it mean it's actually referencing the exception message from the api instead of my wrapper?

Aucun commentaire:

Enregistrer un commentaire