Test Overview
This test uses @pytest.mark.xfail to mark a test that is expected to fail. It verifies that the test fails as expected without causing the whole test suite to fail.
This test uses @pytest.mark.xfail to mark a test that is expected to fail. It verifies that the test fails as expected without causing the whole test suite to fail.
import pytest @pytest.mark.xfail(reason="Known bug: division by zero") def test_divide_by_zero(): result = 10 / 0 assert result == 0
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | PyTest test runner initializes and loads test_divide_by_zero | — | PASS |
| 2 | Test function test_divide_by_zero is called | Inside test_divide_by_zero function | — | PASS |
| 3 | Code executes division 10 / 0 causing ZeroDivisionError | Exception ZeroDivisionError raised | — | FAIL |
| 4 | PyTest recognizes test is marked xfail and failure is expected | Test marked as expected failure | Test failure matches expected failure condition | PASS |
| 5 | Test suite continues without marking overall run as failed | Test suite summary shows xfail for this test | Test suite reports test as xfailed, not failed | PASS |
@pytest.mark.xfail in a test?@pytest.mark.xfail with parentheses for parameters.strict=True means pytest will fail the test if it unexpectedly passes.@pytest.mark.xfail(strict=True)
def test_divide():
assert 1 / 0 == 0@pytest.mark.xfail(strict=True)
def test_sum():
assert sum([1, 2, 3]) == 5strict=True is needed.@pytest.mark.xfail(strict=True, reason='Flaky API') clearly documents the expected failure and enforces strict checking.Pass the test and this pattern moves unknown