Some tweaks

This commit is contained in:
Ran Benita 2024-01-03 18:28:26 +02:00
parent 6bb20d9ead
commit b559724a82
4 changed files with 60 additions and 24 deletions

View File

@ -1 +1 @@
Fix reporting of teardown errors in session and module scoped fixtures when `--maxfail=1`. Fix reporting of teardown errors in higher-scoped fixtures when using `--maxfail` or `--stepwise`.

View File

@ -584,7 +584,8 @@ class Session(nodes.Collector):
@shouldstop.setter @shouldstop.setter
def shouldstop(self, value: Union[bool, str]) -> None: def shouldstop(self, value: Union[bool, str]) -> None:
"""Prevent plugins from setting this to False once it has been set.""" # The runner checks shouldfail and assumes that if it is set we are
# definitely stopping, so prevent unsetting it.
if value is False and self._shouldstop: if value is False and self._shouldstop:
warnings.warn( warnings.warn(
PytestWarning( PytestWarning(
@ -601,7 +602,8 @@ class Session(nodes.Collector):
@shouldfail.setter @shouldfail.setter
def shouldfail(self, value: Union[bool, str]) -> None: def shouldfail(self, value: Union[bool, str]) -> None:
"""Prevent plugins from setting this to False once it has been set.""" # The runner checks shouldfail and assumes that if it is set we are
# definitely stopping, so prevent unsetting it.
if value is False and self._shouldfail: if value is False and self._shouldfail:
warnings.warn( warnings.warn(
PytestWarning( PytestWarning(

View File

@ -1102,22 +1102,21 @@ def test_teardown_session_failed(pytester: Pytester) -> None:
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def baz(): def baz():
yield yield
pytest.fail("This is a failing fixture") pytest.fail("This is a failing teardown")
def test_foo(baz): def test_foo(baz):
pytest.fail("This is a failing test") pytest.fail("This is a failing test")
def test_bar(baz): def test_bar(): pass
pass """
"""
) )
result = pytester.runpytest("--maxfail=1") result = pytester.runpytest("--maxfail=1")
result.assert_outcomes(failed=1, errors=1) result.assert_outcomes(failed=1, errors=1)
def test_teardown_session_stopped(pytester: Pytester) -> None: def test_teardown_session_stopped(pytester: Pytester) -> None:
"""Similar to `test_teardown_session_failed`, but uses `pytest.exit` instead """Test that higher-scoped fixture teardowns run in the context of the last
of `pytest.fail`. item after the test session bails early due to --stepwise.
Regression test for #11706. Regression test for #11706.
""" """
@ -1128,14 +1127,13 @@ def test_teardown_session_stopped(pytester: Pytester) -> None:
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def baz(): def baz():
yield yield
pytest.exit("This is an exiting fixture") pytest.fail("This is a failing teardown")
def test_foo(baz): def test_foo(baz):
pytest.fail("This is a failing test") pytest.fail("This is a failing test")
def test_bar(baz): def test_bar(): pass
pass """
"""
) )
result = pytester.runpytest("--maxfail=1") result = pytester.runpytest("--stepwise")
result.assert_outcomes(failed=1, errors=0) result.assert_outcomes(failed=1, errors=1)

View File

@ -421,14 +421,18 @@ def test_rootdir_wrong_option_arg(pytester: Pytester) -> None:
def test_shouldfail_is_sticky(pytester: Pytester) -> None: def test_shouldfail_is_sticky(pytester: Pytester) -> None:
"""Test that session.shouldfail cannot be reset to False after being set.""" """Test that session.shouldfail cannot be reset to False after being set.
Issue #11706.
"""
pytester.makeconftest( pytester.makeconftest(
""" """
def pytest_sessionfinish(session, exitstatus): def pytest_sessionfinish(session):
assert session.shouldfail
session.shouldfail = False session.shouldfail = False
""" assert session.shouldfail
"""
) )
pytester.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -436,9 +440,41 @@ def test_shouldfail_is_sticky(pytester: Pytester) -> None:
def test_foo(): def test_foo():
pytest.fail("This is a failing test") pytest.fail("This is a failing test")
def test_bar(): def test_bar(): pass
pass """
"""
) )
result = pytester.runpytest("--maxfail=1")
result.stderr.fnmatch_lines("*session.shouldfail cannot be unset*") result = pytester.runpytest("--maxfail=1", "-Wall")
result.assert_outcomes(failed=1, warnings=1)
result.stdout.fnmatch_lines("*session.shouldfail cannot be unset*")
def test_shouldstop_is_sticky(pytester: Pytester) -> None:
"""Test that session.shouldstop cannot be reset to False after being set.
Issue #11706.
"""
pytester.makeconftest(
"""
def pytest_sessionfinish(session):
assert session.shouldstop
session.shouldstop = False
assert session.shouldstop
"""
)
pytester.makepyfile(
"""
import pytest
def test_foo():
pytest.fail("This is a failing test")
def test_bar(): pass
"""
)
result = pytester.runpytest("--stepwise", "-Wall")
result.assert_outcomes(failed=1, warnings=1)
result.stdout.fnmatch_lines("*session.shouldstop cannot be unset*")