Skip to content

Keep higher-scoped fixtures alive across re-runs of failed subtests - #357

Merged
icemac merged 2 commits into
pytest-dev:masterfrom
teddytennant:fix-subtest-premature-teardown
Sep 1, 2026
Merged

Keep higher-scoped fixtures alive across re-runs of failed subtests#357
icemac merged 2 commits into
pytest-dev:masterfrom
teddytennant:fix-subtest-premature-teardown

Conversation

@teddytennant

Copy link
Copy Markdown
Contributor

Follow-up to #351, the second of the two leaks I mentioned there. Independent of #356, though both touch the same condition in pytest_runtest_teardown, so whichever lands second wants a one line rebase. Happy to do it.

A call phase that fails through subtests only leaves the call report itself passing, so every entry in _test_failed_statuses is false. pytest_runtest_teardown reads that as a finished test and lets the module, class and session finalizers run, while _should_not_rerun does count the failed subtests and re-runs the test anyway. The higher-scoped fixtures are torn down and set up again on every attempt:

@pytest.fixture(scope="module", autouse=True)
def module_fixture():
    print("module setup")
    yield
    print("module teardown")

attempts = []

def test_subtests(subtests):
    attempts.append(1)
    with subtests.test("fails twice"):
        assert len(attempts) > 2

With --reruns 3 that prints three setups and three teardowns. The same test failing normally, without subtests, prints one of each.

The fix counts the failed subtests in the teardown check, the same way the re-run decision already does. _get_num_failed_subtests takes a nodeid now instead of a report, since the teardown hook has no report to hand it.

Tests cover class, module and session scope and the cross-module case. One is a guard rather than a reproduction: subtests that never pass exhaust the re-runs and still tear the module fixture down exactly once.

@teddytennant
teddytennant force-pushed the fix-subtest-premature-teardown branch from a3b028e to 5e87576 Compare August 27, 2026 12:45

@icemac icemac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed with a full diff pass plus runtime verification against pytest 9.1.1. The change does what it targets — subtest-only failures now keep module/class/session fixtures alive, and the pytest.skip()-after-failed-subtest path still re-runs without leaking. All 178 existing tests plus the 5 new ones pass, and the new tests themselves look right (module ordering is alphabetically stable; temporary_failure's test.res marker is per-tmpdir, so the scope parametrization does not cross-contaminate).

One regression, in the comment below.

Two things I looked at and am not filing:

  • --only-rerun / --rerun-except are bypassed for subtest-only failures, because _should_hard_fail_on_error returns early when the call report's outcome is passed. Teardown and the re-run decision agree there, so no finalizer leak — pre-existing behaviour from the subtests support, not this PR.
  • The failed_subtests stash entry is never deleted on the final, non-re-run attempt. Harmless today: teardown only reads the current item's nodeid, and each nodeid runs one protocol per session.

Comment created by Claude

Comment thread src/pytest_rerunfailures.py
@teddytennant
teddytennant force-pushed the fix-subtest-premature-teardown branch from 5e87576 to 71806f6 Compare August 31, 2026 12:06
@icemac
icemac enabled auto-merge (squash) September 1, 2026 05:57
@icemac
icemac merged commit 367df90 into pytest-dev:master Sep 1, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants