From 00d9640abc783e714881a2380af22ed77462ab21 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 23 Feb 2024 11:29:00 +0200 Subject: [PATCH] Revert "Fix teardown error reporting when `--maxfail=1` (#11721)" Fix #12021. Reopens #11706. This reverts commit 12b9bd580198edf88a795b692cbd6a1a05fe408a. This change caused a bad regression in pytest-xdist: https://github.com/pytest-dev/pytest-xdist/issues/1024 pytest-xdist necessarily has special handling of `--maxfail` and session fixture teardown get executed multiple times with the change. Since I'm not sure how to adapt pytest-xdist myself, revert for now. I kept the sticky `shouldstop`/`shouldfail` changes as they are good ideas regardless I think. --- changelog/12021.bugfix.rst | 1 + doc/en/changelog.rst | 2 ++ src/_pytest/runner.py | 4 --- testing/test_runner.py | 50 -------------------------------------- 4 files changed, 3 insertions(+), 54 deletions(-) create mode 100644 changelog/12021.bugfix.rst diff --git a/changelog/12021.bugfix.rst b/changelog/12021.bugfix.rst new file mode 100644 index 00000000000..eb8771ad720 --- /dev/null +++ b/changelog/12021.bugfix.rst @@ -0,0 +1 @@ +Reverted a fix to `--maxfail` handling in pytest 8.0.0 because it caused a regression in pytest-xdist whereby session fixture teardowns may get executed multiple times when the max-fails is reached. diff --git a/doc/en/changelog.rst b/doc/en/changelog.rst index 6ed764207e6..88f67635565 100644 --- a/doc/en/changelog.rst +++ b/doc/en/changelog.rst @@ -85,6 +85,8 @@ Bug Fixes - `#11706 `_: Fix reporting of teardown errors in higher-scoped fixtures when using `--maxfail` or `--stepwise`. + NOTE: This change was reverted in pytest 8.0.2 to fix a `regression `_ it caused in pytest-xdist. + - `#11758 `_: Fixed ``IndexError: string index out of range`` crash in ``if highlighted[-1] == "\n" and source[-1] != "\n"``. This bug was introduced in pytest 8.0.0rc1. diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index d25fdb73836..7ed8f09bdbe 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -132,10 +132,6 @@ def runtestprotocol( show_test_item(item) if not item.config.getoption("setuponly", False): reports.append(call_and_report(item, "call", log)) - # If the session is about to fail or stop, teardown everything - this is - # necessary to correctly report fixture teardown errors (see #11706) - if item.session.shouldfail or item.session.shouldstop: - nextitem = None reports.append(call_and_report(item, "teardown", log, nextitem=nextitem)) # After all teardown hooks have been called # want funcargs and request info to go away. diff --git a/testing/test_runner.py b/testing/test_runner.py index 6b2b3105b0c..322e6dc0462 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1089,53 +1089,3 @@ def func() -> None: with pytest.raises(TypeError) as excinfo: OutcomeException(func) # type: ignore assert str(excinfo.value) == expected - - -def test_teardown_session_failed(pytester: Pytester) -> None: - """Test that higher-scoped fixture teardowns run in the context of the last - item after the test session bails early due to --maxfail. - - Regression test for #11706. - """ - pytester.makepyfile( - """ - import pytest - - @pytest.fixture(scope="module") - def baz(): - yield - pytest.fail("This is a failing teardown") - - def test_foo(baz): - pytest.fail("This is a failing test") - - def test_bar(): pass - """ - ) - result = pytester.runpytest("--maxfail=1") - result.assert_outcomes(failed=1, errors=1) - - -def test_teardown_session_stopped(pytester: Pytester) -> None: - """Test that higher-scoped fixture teardowns run in the context of the last - item after the test session bails early due to --stepwise. - - Regression test for #11706. - """ - pytester.makepyfile( - """ - import pytest - - @pytest.fixture(scope="module") - def baz(): - yield - pytest.fail("This is a failing teardown") - - def test_foo(baz): - pytest.fail("This is a failing test") - - def test_bar(): pass - """ - ) - result = pytester.runpytest("--stepwise") - result.assert_outcomes(failed=1, errors=1)