Skip to content

recwarn: let base exceptions propagate through pytest.warns again #11920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/11907.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a regression in pytest 8.0.0 whereby calling :func:`pytest.skip` and similar control-flow exceptions within a :func:`pytest.warns()` block would get suppressed instead of propagating.
12 changes: 12 additions & 0 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from _pytest.deprecated import check_ispytest
from _pytest.fixtures import fixture
from _pytest.outcomes import Exit
from _pytest.outcomes import fail


Expand Down Expand Up @@ -302,6 +303,17 @@ def __exit__(

__tracebackhide__ = True

# BaseExceptions like pytest.{skip,fail,xfail,exit} or Ctrl-C within
# pytest.warns should *not* trigger "DID NOT WARN" and get suppressed
# when the warning doesn't happen. Control-flow exceptions should always
# propagate.
Comment on lines +306 to +309
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, technically I see some packages (trio) using with pytest.raises(GeneratorExit) but it's a corner case, so I'm not going to argue.

if exc_val is not None and (
not isinstance(exc_val, Exception)
# Exit is an Exception, not a BaseException, for some reason.
or isinstance(exc_val, Exit)
):
return

def found_str():
return pformat([record.message for record in self], indent=2)

Expand Down
70 changes: 68 additions & 2 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from typing import Type
import warnings

from _pytest.pytester import Pytester
from _pytest.recwarn import WarningsRecorder
import pytest
from pytest import ExitCode
from pytest import Pytester
from pytest import WarningsRecorder


def test_recwarn_stacklevel(recwarn: WarningsRecorder) -> None:
Expand Down Expand Up @@ -479,6 +480,71 @@ def test_catch_warning_within_raise(self) -> None:
warnings.warn("some warning", category=FutureWarning)
raise ValueError("some exception")

def test_skip_within_warns(self, pytester: Pytester) -> None:
"""Regression test for #11907."""
pytester.makepyfile(
"""
import pytest

def test_it():
with pytest.warns(Warning):
pytest.skip("this is OK")
""",
)

result = pytester.runpytest()
assert result.ret == ExitCode.OK
result.assert_outcomes(skipped=1)

def test_fail_within_warns(self, pytester: Pytester) -> None:
"""Regression test for #11907."""
pytester.makepyfile(
"""
import pytest

def test_it():
with pytest.warns(Warning):
pytest.fail("BOOM")
""",
)

result = pytester.runpytest()
assert result.ret == ExitCode.TESTS_FAILED
result.assert_outcomes(failed=1)
assert "DID NOT WARN" not in str(result.stdout)

def test_exit_within_warns(self, pytester: Pytester) -> None:
"""Regression test for #11907."""
pytester.makepyfile(
"""
import pytest

def test_it():
with pytest.warns(Warning):
pytest.exit()
""",
)

result = pytester.runpytest()
assert result.ret == ExitCode.INTERRUPTED
result.assert_outcomes()

def test_keyboard_interrupt_within_warns(self, pytester: Pytester) -> None:
"""Regression test for #11907."""
pytester.makepyfile(
"""
import pytest

def test_it():
with pytest.warns(Warning):
raise KeyboardInterrupt()
""",
)

result = pytester.runpytest_subprocess()
assert result.ret == ExitCode.INTERRUPTED
result.assert_outcomes()


def test_raise_type_error_on_non_string_warning() -> None:
"""Check pytest.warns validates warning messages are strings (#10865)."""
Expand Down