Expecting Tests to Fail with pytest.mark.xfail

If we want to run all tests, even those that we know will fail, we can use the xfail marker.

Here’s the full signature for xfail:

 @pytest.mark.xfail(condition, ..., *, reason, run=True,
 raises=None, strict=xfail_strict)

The first set of parameters to this fixture are the same as skipif. The test is run anyway, by default, but the run parameter can be used to tell pytest to not run the test by setting run=False. The raises parameter allows you to provide an exception type or a tuple of exception types that you want to result in an xfail. Any other exception will cause the test to fail. strict tells pytest if passing tests should be marked as XPASS (strict=False) or FAIL, strict=True.

Let’s look ...

Get Python Testing with pytest now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.