Correctly report teardown fixture errors

This commit is contained in:
Ben Brown 2023-12-18 11:28:24 -05:00 committed by Ran Benita
parent f017df443a
commit 7176470866
4 changed files with 27 additions and 0 deletions

View File

@ -54,6 +54,7 @@ Aviral Verma
Aviv Palivoda
Babak Keyvani
Barney Gale
Ben Brown
Ben Gartner
Ben Webb
Benjamin Peterson

View File

@ -0,0 +1 @@
Fix reporting of teardown errors in session and module scoped fixtures when `--maxfail=1`.

View File

@ -179,6 +179,9 @@ def pytest_runtest_call(item: Item) -> None:
def pytest_runtest_teardown(item: Item, nextitem: Optional[Item]) -> None:
_update_current_test_var(item, "teardown")
# If the session is about to fail, teardown everything - this is necessary
# to correctly report fixture teardown errors (see #11706)
nextitem = None if item.session.shouldfail else nextitem
item.session._setupstate.teardown_exact(nextitem)
_update_current_test_var(item, None)

View File

@ -1087,3 +1087,25 @@ def test_outcome_exception_bad_msg() -> 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 fixture teardown failures are reported after a test fails."""
pytester.makepyfile(
"""
import pytest
@pytest.fixture(scope="module")
def baz():
yield
pytest.fail("This is a failing fixture")
def test_foo(baz):
pytest.fail("This is a failing test")
def test_bar(baz):
pass
"""
)
result = pytester.runpytest("--maxfail=1")
result.stdout.fnmatch_lines(["*1 failed, 1 error*"])