Skip to content

Conversation

@FazeelUsmani
Copy link

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:

  • Output: 1 passed, 2 xfailed (incorrect - duplicate xfail)
  • Session fixture teardown exceptions were being treated as expected test failures

After this fix:

  • Output: 1 passed, 1 xfailed, 1 error (correct)
  • Session fixture teardown exceptions are properly reported as errors

Root Cause

The xfail handling in pytest_runtest_makereport was 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

  • Modified src/_pytest/skipping.py: Added if call.when == "call" condition to xfail handling
  • Added regression test: test_session_fixture_teardown_exception_with_xfail
  • Updated 4 existing tests to reflect correct behavior (errors instead of xfails for setup/teardown failures)

FazeelUsmani and others added 2 commits November 10, 2025 14:55
…FAILs

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.

The root cause was that the xfail handling in pytest_runtest_makereport was
being applied to all phases (setup, call, teardown), converting any exception
into an xfail result if the test was marked with xfail. This meant that
session fixture teardown exceptions were being misreported as expected failures.

The fix restricts 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, not to fixture setup/teardown failures.

Fixes pytest-dev#8375
@FazeelUsmani FazeelUsmani force-pushed the fix-session-fixture-teardown-xfail-011CUtYnq316CxJsBDUpkHnm branch 4 times, most recently from e105874 to 78bf7fe Compare November 10, 2025 10:15
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 10, 2025
@FazeelUsmani FazeelUsmani force-pushed the fix-session-fixture-teardown-xfail-011CUtYnq316CxJsBDUpkHnm branch 2 times, most recently from fc068e8 to 55005ee Compare November 10, 2025 10:19
@The-Compiler
Copy link
Member

Duplicate of #13886 (and frankly, that branch being named FazeelUsmani:claude/update-commit-author-011CUth8nk5b4ZT9xscUXzwv is... quite telling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error not raised in session fixture when mark.xfail is set on last test

2 participants