0
0
PyTesttesting~10 mins

@pytest.mark.xfail for expected failures - Test Execution Trace

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

Test Code - PyTest
PyTest
import pytest

@pytest.mark.xfail(reason="Known bug: division by zero")
def test_divide_by_zero():
    result = 10 / 0
    assert result == 0
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsPyTest test runner initializes and loads test_divide_by_zero-PASS
2Test function test_divide_by_zero is calledInside test_divide_by_zero function-PASS
3Code executes division 10 / 0 causing ZeroDivisionErrorException ZeroDivisionError raised-FAIL
4PyTest recognizes test is marked xfail and failure is expectedTest marked as expected failureTest failure matches expected failure conditionPASS
5Test suite continues without marking overall run as failedTest suite summary shows xfail for this testTest suite reports test as xfailed, not failedPASS
Failure Scenario
Failing Condition: Test marked xfail unexpectedly passes (no error raised)
Execution Trace Quiz - 3 Questions
Test your understanding
What does @pytest.mark.xfail do in this test?
AMarks the test as expected to fail without failing the suite
BSkips the test completely
CMakes the test always pass
DRuns the test twice
Key Result
Use @pytest.mark.xfail to mark tests that are known to fail due to bugs or incomplete features. This helps keep the test suite green while tracking expected failures.