@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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.