Skip to content
Closed
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/9288.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pytest.warns`` no longer suppresses warnings derived from ``DeprecationWarning`` and ``PendingDeprecationWarning`` by default.
9 changes: 9 additions & 0 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ def found_str():

# only check if we're not currently handling an exception
if exc_type is None and exc_val is None and exc_tb is None:
for w in self:
reemit = {DeprecationWarning, PendingDeprecationWarning}
if self.expected_warning is not None:
reemit -= set(self.expected_warning)
if issubclass(w.category, tuple(reemit)):
warnings.showwarning(
w.message, w.category, w.filename, w.lineno, w.file, w.line
)

if self.expected_warning is not None:
if not any(issubclass(r.category, self.expected_warning) for r in self):
__tracebackhide__ = True
Expand Down
22 changes: 22 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ def test_invalid_enter_exit(self) -> None:
with rec:
pass # can't enter twice

def test_no_suppress_deprecation_warnings(self) -> None:
with warnings.catch_warnings(record=True) as record:
warnings.simplefilter("always")
with pytest.warns(DeprecationWarning), pytest.warns(
PendingDeprecationWarning
):
with pytest.warns(UserWarning):
warnings.warn("my warning", UserWarning)
warnings.warn("some deprecation warning", DeprecationWarning)
warnings.warn(
"other deprecation warning", PendingDeprecationWarning
)
assert len(record) == 0


class TestDeprecatedCall:
"""test pytest.deprecated_call()"""
Expand Down Expand Up @@ -217,6 +231,14 @@ def test_deprecated_call_supports_match(self) -> None:
with pytest.deprecated_call(match=r"must be \d+$"):
warnings.warn("this is not here", DeprecationWarning)

def test_suppress_deprecation_warnings(self) -> None:
with warnings.catch_warnings(record=True) as record:
warnings.simplefilter("always")
with pytest.deprecated_call():
warnings.warn("some deprecation warning", DeprecationWarning)
warnings.warn("other deprecation warning", PendingDeprecationWarning)
assert len(record) == 0


class TestWarns:
def test_check_callable(self) -> None:
Expand Down