Keep higher-scoped fixtures alive across re-runs of failed subtests - #357
Merged
icemac merged 2 commits intoSep 1, 2026
Merged
Conversation
teddytennant
force-pushed
the
fix-subtest-premature-teardown
branch
from
August 27, 2026 12:45
a3b028e to
5e87576
Compare
icemac
reviewed
Aug 31, 2026
icemac
left a comment
Contributor
There was a problem hiding this comment.
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-exceptare bypassed for subtest-only failures, because_should_hard_fail_on_errorreturns early when the call report's outcome ispassed. Teardown and the re-run decision agree there, so no finalizer leak — pre-existing behaviour from the subtests support, not this PR.- The
failed_subtestsstash 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
teddytennant
force-pushed
the
fix-subtest-premature-teardown
branch
from
August 31, 2026 12:06
5e87576 to
71806f6
Compare
icemac
enabled auto-merge (squash)
September 1, 2026 05:57
icemac
approved these changes
Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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_statusesis false.pytest_runtest_teardownreads that as a finished test and lets the module, class and session finalizers run, while_should_not_rerundoes count the failed subtests and re-runs the test anyway. The higher-scoped fixtures are torn down and set up again on every attempt:With
--reruns 3that 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_subteststakes 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.