From 1f0fcba72c4b47b7595c3df60303316c107e5c8f Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Thu, 5 Feb 2026 19:35:55 +0200 Subject: [PATCH 1/2] Preserve session and module teardown when fixture teardown fails Signed-off-by: Emmanuel Ferdman --- src/pytest_rerunfailures.py | 6 +++--- tests/test_pytest_rerunfailures.py | 34 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/pytest_rerunfailures.py b/src/pytest_rerunfailures.py index c344d12..63223ae 100644 --- a/src/pytest_rerunfailures.py +++ b/src/pytest_rerunfailures.py @@ -250,11 +250,11 @@ def _remove_failed_setup_state_from_session(item): """ Clean up setup state. - Note: remove all failures from every node in _setupstate stack - and clean the stack itself + Note: remove only the current item, not higher-scoped items """ setup_state = item.session._setupstate - setup_state.stack = {} + if item in setup_state.stack: + del setup_state.stack[item] def _get_rerun_filter_regex(item, regex_name): diff --git a/tests/test_pytest_rerunfailures.py b/tests/test_pytest_rerunfailures.py index 4220a98..7156bc9 100644 --- a/tests/test_pytest_rerunfailures.py +++ b/tests/test_pytest_rerunfailures.py @@ -1170,6 +1170,40 @@ def test_2(): assert_outcomes(result, failed=8, passed=2, rerun=18, skipped=5, error=1) +def test_run_session_teardown_when_fixture_teardown_fails(testdir): + testdir.makepyfile( + """ + import pytest + + @pytest.fixture(scope='session', autouse=True) + def session_fixture(): + yield + print('session teardown') + + @pytest.fixture(scope='module', autouse=True) + def module_fixture(): + yield + print('module teardown') + + @pytest.fixture + def broken_fixture(): + yield + raise Exception("fixture teardown error") + + def test_fail_in_fixture(broken_fixture): + pass + + def test_ok(): + pass + """ + ) + + result = testdir.runpytest("--reruns", "1", "-s") + result.stdout.fnmatch_lines("*session teardown*") + result.stdout.fnmatch_lines("*module teardown*") + assert_outcomes(result, passed=3, rerun=1, error=1) + + def test_exception_matches_rerun_except_query(testdir): testdir.makepyfile( """ From 5458d0bc4afd90a8d6889daa1d2770b17ce95e59 Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Fri, 6 Feb 2026 12:50:10 +0200 Subject: [PATCH 2/2] Preserve session and module teardown when fixture teardown fails Signed-off-by: Emmanuel Ferdman --- CHANGES.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index dd6389d..8638e1c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,8 @@ Changelog 16.2 (unreleased) ----------------- -- Nothing changed yet. +- Fix missing teardown for session and module scoped fixtures when fixture teardown fails. + Fixes `#314 `_. 16.1 (2025-10-10)