Test Overview
This test demonstrates how pytest built-in markers skip, skipif, and xfail control test execution. It verifies that tests are skipped or expected to fail under certain conditions.
This test demonstrates how pytest built-in markers skip, skipif, and xfail control test execution. It verifies that tests are skipped or expected to fail under certain conditions.
import pytest @pytest.mark.skip(reason="Skipping this test always") def test_always_skipped(): assert False # This should never run @pytest.mark.skipif(True, reason="Condition is True, so skip") def test_skipped_if_true(): assert False # This should be skipped @pytest.mark.xfail(reason="Known bug, expected failure") def test_expected_failure(): assert 1 == 2 # This test is expected to fail @pytest.mark.xfail(reason="Known bug, but fixed now", strict=True) def test_strict_xfail(): assert 1 == 1 # This test should pass, so strict xfail will fail the test
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | pytest starts test run | pytest test runner initialized | — | PASS |
| 2 | pytest discovers test_always_skipped and applies @pytest.mark.skip | test_always_skipped marked to skip with reason | — | PASS |
| 3 | pytest skips test_always_skipped without running it | test_always_skipped skipped | test_always_skipped is skipped | PASS |
| 4 | pytest discovers test_skipped_if_true and applies @pytest.mark.skipif with condition True | test_skipped_if_true marked to skip if condition is True | — | PASS |
| 5 | pytest skips test_skipped_if_true because condition is True | test_skipped_if_true skipped | test_skipped_if_true is skipped | PASS |
| 6 | pytest discovers test_expected_failure and applies @pytest.mark.xfail | test_expected_failure marked as expected failure | — | PASS |
| 7 | pytest runs test_expected_failure which asserts 1 == 2 (fails) | test_expected_failure executed and failed | test_expected_failure failure matches xfail expectation | PASS |
| 8 | pytest marks test_expected_failure as xfail (expected failure) | test_expected_failure result recorded as xfail | test_expected_failure is reported as expected failure | PASS |
| 9 | pytest discovers test_strict_xfail and applies @pytest.mark.xfail with strict=True | test_strict_xfail marked as strict xfail | — | PASS |
| 10 | pytest runs test_strict_xfail which asserts 1 == 1 (passes) | test_strict_xfail executed and passed | test_strict_xfail passed but marked strict xfail | FAIL |
| 11 | pytest reports test_strict_xfail as failure because strict xfail expects failure | test_strict_xfail result recorded as failure | strict xfail test passed unexpectedly, causing failure | FAIL |
@pytest.mark.skip marker do in a test?@pytest.mark.skipskipif skips conditionally, and xfail marks expected failures but runs the test. So only skip always skips.@pytest.mark.skip always skips = D [OK]skipif with reasonimport pytest
@pytest.mark.xfail
def test_example():
assert 1 == 2@pytest.mark.xfail behavior1 == 2 is false, so the test fails. Because of xfail, this failure is expected and reported accordingly.import pytest
@pytest.mark.skipif(sys.platform == 'win32')
def test_func():
assert Truesys.platform, but sys is not imported, causing a NameError at runtime.skipif and xfail markers?skipif for Windows must be applied first.xfail marker is applied second to mark expected failures on old Python versions.skipif first, then xfail, both with reasons. This is the correct and clear approach.Pass the test and this pattern moves unknown