0
0
PyTesttesting~10 mins

Built-in markers (skip, skipif, xfail) in PyTest - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - pytest
PyTest
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
Execution Trace - 11 Steps
StepActionSystem StateAssertionResult
1pytest starts test runpytest test runner initialized-PASS
2pytest discovers test_always_skipped and applies @pytest.mark.skiptest_always_skipped marked to skip with reason-PASS
3pytest skips test_always_skipped without running ittest_always_skipped skippedtest_always_skipped is skippedPASS
4pytest discovers test_skipped_if_true and applies @pytest.mark.skipif with condition Truetest_skipped_if_true marked to skip if condition is True-PASS
5pytest skips test_skipped_if_true because condition is Truetest_skipped_if_true skippedtest_skipped_if_true is skippedPASS
6pytest discovers test_expected_failure and applies @pytest.mark.xfailtest_expected_failure marked as expected failure-PASS
7pytest runs test_expected_failure which asserts 1 == 2 (fails)test_expected_failure executed and failedtest_expected_failure failure matches xfail expectationPASS
8pytest marks test_expected_failure as xfail (expected failure)test_expected_failure result recorded as xfailtest_expected_failure is reported as expected failurePASS
9pytest discovers test_strict_xfail and applies @pytest.mark.xfail with strict=Truetest_strict_xfail marked as strict xfail-PASS
10pytest runs test_strict_xfail which asserts 1 == 1 (passes)test_strict_xfail executed and passedtest_strict_xfail passed but marked strict xfailFAIL
11pytest reports test_strict_xfail as failure because strict xfail expects failuretest_strict_xfail result recorded as failurestrict xfail test passed unexpectedly, causing failureFAIL
Failure Scenario
Failing Condition: test marked with strict xfail passes unexpectedly
Execution Trace Quiz - 3 Questions
Test your understanding
What happens to a test marked with @pytest.mark.skip?
AThe test is not run and marked as skipped
BThe test runs but is expected to fail
CThe test runs and must pass
DThe test runs and is ignored if it fails
Key Result
Use pytest markers skip, skipif, and xfail to control test execution flow and handle known issues clearly. Strict xfail helps detect when a bug is fixed but the marker was not updated.