Skip to content

Commit 4c5298c

Browse files
authored
Add back "Fix teardown error reporting when --maxfail=1 (#11721)" (#12279)
Closes #11706. Originally fixed in #11721, but then reverted in #12022 due to a regression in pytest-xdist. The regression was fixed on the pytest-xdist side in pytest-dev/pytest-xdist#1026.
1 parent c3e9bd4 commit 4c5298c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

changelog/11706.bugfix.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix reporting of teardown errors in higher-scoped fixtures when using `--maxfail` or `--stepwise`.
2+
3+
Originally added in pytest 8.0.0, but reverted in 8.0.2 due to a regression in pytest-xdist.
4+
This regression was fixed in pytest-xdist 3.6.1.

src/_pytest/runner.py

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ def runtestprotocol(
134134
show_test_item(item)
135135
if not item.config.getoption("setuponly", False):
136136
reports.append(call_and_report(item, "call", log))
137+
# If the session is about to fail or stop, teardown everything - this is
138+
# necessary to correctly report fixture teardown errors (see #11706)
139+
if item.session.shouldfail or item.session.shouldstop:
140+
nextitem = None
137141
reports.append(call_and_report(item, "teardown", log, nextitem=nextitem))
138142
# After all teardown hooks have been called
139143
# want funcargs and request info to go away.

testing/test_runner.py

+50
Original file line numberDiff line numberDiff line change
@@ -1216,3 +1216,53 @@ def test():
12161216
result = pytester.runpytest_inprocess()
12171217
assert result.ret == ExitCode.OK
12181218
assert os.environ["PYTEST_VERSION"] == "old version"
1219+
1220+
1221+
def test_teardown_session_failed(pytester: Pytester) -> None:
1222+
"""Test that higher-scoped fixture teardowns run in the context of the last
1223+
item after the test session bails early due to --maxfail.
1224+
1225+
Regression test for #11706.
1226+
"""
1227+
pytester.makepyfile(
1228+
"""
1229+
import pytest
1230+
1231+
@pytest.fixture(scope="module")
1232+
def baz():
1233+
yield
1234+
pytest.fail("This is a failing teardown")
1235+
1236+
def test_foo(baz):
1237+
pytest.fail("This is a failing test")
1238+
1239+
def test_bar(): pass
1240+
"""
1241+
)
1242+
result = pytester.runpytest("--maxfail=1")
1243+
result.assert_outcomes(failed=1, errors=1)
1244+
1245+
1246+
def test_teardown_session_stopped(pytester: Pytester) -> None:
1247+
"""Test that higher-scoped fixture teardowns run in the context of the last
1248+
item after the test session bails early due to --stepwise.
1249+
1250+
Regression test for #11706.
1251+
"""
1252+
pytester.makepyfile(
1253+
"""
1254+
import pytest
1255+
1256+
@pytest.fixture(scope="module")
1257+
def baz():
1258+
yield
1259+
pytest.fail("This is a failing teardown")
1260+
1261+
def test_foo(baz):
1262+
pytest.fail("This is a failing test")
1263+
1264+
def test_bar(): pass
1265+
"""
1266+
)
1267+
result = pytester.runpytest("--stepwise")
1268+
result.assert_outcomes(failed=1, errors=1)

0 commit comments

Comments
 (0)