@pytest.mark.xfail

You can use @pytest.mark.xfail decorator to indicate that a test is expected to fail. As usual, we apply the mark decorator to the test function or method:

@pytest.mark.xfaildef test_simulation_34():    ...

This mark supports some parameters, all of which we will see later in this section; but one in particular warrants discussion now: the strict parameter. This parameter defines two distinct behaviors for the mark:

  • With strict=False (the default), the test will be counted separately as an XPASS (if it passes) or an XFAIL (if it fails), and will not fail the test suite
  • With strict=True, the test will be marked as XFAIL if it fails, but if it unexpectedly passes, it will fail the test suite, as a normal failing test would ...

Get pytest Quick Start Guide 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.