Keep higher-scoped fixtures alive across re-runs of failed subtests - #357
Open
teddytennant wants to merge 1 commit into
Open
Keep higher-scoped fixtures alive across re-runs of failed subtests#357teddytennant wants to merge 1 commit into
teddytennant wants to merge 1 commit into
Conversation
teddytennant
force-pushed
the
fix-subtest-premature-teardown
branch
from
August 27, 2026 12:45
a3b028e to
5e87576
Compare
A call phase that only has failed subtests leaves the call report itself passing, so the failure statuses pytest_runtest_teardown looks at are all false. The hook then treats the test as finished and lets the module, class and session finalizers run, while the protocol goes on to re-run the test. The higher-scoped fixtures are torn down and set up again on every attempt. Count the failed subtests in that check, the same way the re-run decision already does.
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.