Fix fixture finalizers being skipped when setup is interrupted - #15092
Closed
Dextheking1 wants to merge 4 commits into
Closed
Dextheking1 wants to merge 4 commits into
Dextheking1 wants to merge 4 commits into
Conversation
Record interruptions (e.g. KeyboardInterrupt) that escape pytest_fixture_setup in cached_result so FixtureDef.finish() still runs finalizers registered via request.addfinalizer() during teardown, as documented. Fixes pytest-dev#15067.
for more information, see https://pre-commit.ci
The subprocess regression test cannot be seen by coverage, so the new except BaseException branch in pytest_fixture_setup showed 0% patch coverage. This in-process variant runs the same interrupted-setup scenario in-process (with no_reraise_ctrlc=True) so coverage observes the new branch.
for more information, see https://pre-commit.ci
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.
Fixes #15067.
When fixture setup is interrupted by something that is not a test outcome -- e.g.
KeyboardInterruptfrom a fail-fast plugin or a manual interrupt --pytest_fixture_setupnever assignedcached_result.FixtureDef.finish()treatscached_result is Noneas "already finished" and returns early, so finalizers registered viarequest.addfinalizer()before the interruption were silently skipped, even though the docs say a finalizer runs once added, even if the fixture raises afterwards.The fix records the interruption in
cached_result(a newexcept BaseExceptionbranch alongsideexcept TEST_OUTCOME), so teardown still runs the registered finalizers. On later cache hits the recorded exception is re-raised as before, so no stale value can leak.Testing:
testing/python/fixtures.py::TestRequestBasic::test_request_addfinalizer_interrupted_setupruns an inner pytest in a subprocess whose fixture registers a finalizer and then raisesKeyboardInterrupt; it asserts the run exits interrupted and the finalizer marker file is created. Verified failing before the fix, passing after.testing/python/fixtures.py: 243 passed, 1 skipped, 2 xfailed.Changelog entry added per the towncrier convention (
changelog/15067.bugfix.rst).