Skip to content
All lessons

Tech Insights· docs.pytest.org

pytest makes it easier to flag tests that are expected to fail. It's really useful when in development, e.g., feature not yet implement or bug not totally fixed. Making use of it is as simple as:

import pytest

@pytest.mark.xfail
def test_xpto():
    ...

This way, it'll be presented differently in the terminal report and one can track whether a test is failing as expected or unexpectedly passing.

While the test marked as expected to fail will be presented as xfailed in the test summary, if it passes it'll be displayed as xpassed and when that happens it's not displayed as passed because that test was marked to expect failure.

One can even go further and set a reason to why a test is expected to fail, one can set a reason:

import pytest

@pytest.mark.xfail(reason="feature not totally implemented")
def test_xpto():
    ...

To display this reason in the test summary, one can use the -r option with the corresponding "short" letters shown in progress(x for xfail and/or X for xpass), e.g. pytest -rxX.

For more information, check the documentation at: #xfail-mark-test-functions-as-expected-to-fail

Shared by

Read on docs.pytest.org
  • Pytest

Book a call

Lessons like this one
shape how we build.

Tell us what you’re building. In one call, we’ll show you how our team would approach it.

Tell us what you’re building, and we’ll show you how we’d approach it.

Still prefer the good old email? Contact us