Fix session fixture teardown exceptions being reported as duplicate XFAILs #13886
+77
−21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #8375
When a
session-scoped autouse fixture raises an exception during teardown, and the last test in the suite is marked@pytest.mark.xfail, pytest was incorrectly showing an extra XFAIL line (duplicated) instead of reporting the teardown failure as an ERROR.Problem
Before this fix:
1 passed, 2 xfailed(incorrect - duplicate xfail)After this fix:
1 passed, 1 xfailed, 1 error(correct)Root Cause
The xfail handling in
pytest_runtest_makereportwas being applied to all test phases (setup, call, teardown). This caused any exception during teardown to be converted to an xfail result if the test was marked with@pytest.mark.xfail.Solution
Restrict xfail handling to only apply during the "call" phase. Setup and teardown failures are now properly reported as errors, regardless of xfail markers on the test.
This aligns with the principle that xfail should only apply to test execution logic, not to fixture infrastructure failures (setup/teardown).
Changes
src/_pytest/skipping.py: Addedif call.when == "call"condition to xfail handlingtest_session_fixture_teardown_exception_with_xfail