lundi 1 juillet 2019

How to create an extended xfail marker for py.test?

In py.test (version 4.6.2) you have a mark-decorator for a test to mark it as failed, e.g.

@pytest.mark.xfail
def test1():
    return 1/0

It is also possible to verify the exception itself

@pytest.mark.xfail(raises=ZeroDivisionError)

but is it possible in some way to verify the error message itself?

this is useful when you have a HTTPError, as there can be many reasons. And when you compare the error message itself you can be much more specific about when the test fails (e.g. distinguish a certain Client Error from a Server Error).

I am using the following construct so far:

def test_fail_request(self):      
    with pytest.raises(requests.exceptions.HTTPError) as excinfo:
        response = requests.something
    assert '403 Client Error: Not Found' in str(excinfo.value)

but of course a test like the follows would be more readable, compact and correctly handled by py.test:

 @pytest.mark.xfail(expected_error = "403 Client Error: Not Found"):
 def test_fail_request(self):      
    response = requests.something

Is there a way to implement this behavior/feature?

Aucun commentaire:

Enregistrer un commentaire