Skip to content

Commit c85b21e

Browse files
committed
Remove outdated docs about pytest.warns and DeprecatedWarning
Since #2908, the user doesn't need to set warning filters to capture `DeprecationWarning` with `pytest.warns`. Fix #8666
1 parent fb7e36b commit c85b21e

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

doc/en/how-to/capture-warnings.rst

+3-24
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,6 @@ DeprecationWarning and PendingDeprecationWarning
173173
------------------------------------------------
174174

175175

176-
177-
178176
By default pytest will display ``DeprecationWarning`` and ``PendingDeprecationWarning`` warnings from
179177
user code and third-party libraries, as recommended by `PEP-0565 <https://www.python.org/dev/peps/pep-0565>`_.
180178
This helps users keep their code modern and avoid breakages when deprecated warnings are effectively removed.
@@ -230,27 +228,8 @@ that a certain function call triggers a ``DeprecationWarning`` or
230228
This test will fail if ``myfunction`` does not issue a deprecation warning
231229
when called with a ``17`` argument.
232230

233-
By default, ``DeprecationWarning`` and ``PendingDeprecationWarning`` will not be
234-
caught when using :func:`pytest.warns` or :ref:`recwarn <recwarn>` because
235-
the default Python warnings filters hide
236-
them. If you wish to record them in your own code, use
237-
``warnings.simplefilter('always')``:
238-
239-
.. code-block:: python
240-
241-
import warnings
242-
import pytest
243-
244-
245-
def test_deprecation(recwarn):
246-
warnings.simplefilter("always")
247-
myfunction(17)
248-
assert len(recwarn) == 1
249-
assert recwarn.pop(DeprecationWarning)
250231

251232

252-
The :ref:`recwarn <recwarn>` fixture automatically ensures to reset the warnings
253-
filter at the end of the test, so no global state is leaked.
254233

255234
.. _`asserting warnings`:
256235

@@ -317,9 +296,9 @@ additional information:
317296
Alternatively, you can examine raised warnings in detail using the
318297
:ref:`recwarn <recwarn>` fixture (see below).
319298

320-
.. note::
321-
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
322-
differently; see :ref:`ensuring_function_triggers`.
299+
300+
The :ref:`recwarn <recwarn>` fixture automatically ensures to reset the warnings
301+
filter at the end of the test, so no global state is leaked.
323302

324303
.. _`recording warnings`:
325304

testing/test_recwarn.py

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ def test_method(recwarn):
2727
reprec.assertoutcome(passed=1)
2828

2929

30+
@pytest.mark.filterwarnings("")
31+
def test_recwarn_captures_deprecation_warning(recwarn: WarningsRecorder) -> None:
32+
"""
33+
Check that recwarn can capture DeprecationWarning by default
34+
without custom filterwarnings (see #8666).
35+
"""
36+
warnings.warn(DeprecationWarning("some deprecation"))
37+
assert len(recwarn) == 1
38+
assert recwarn.pop(DeprecationWarning)
39+
40+
3041
class TestWarningsRecorderChecker:
3142
def test_recording(self) -> None:
3243
rec = WarningsRecorder(_ispytest=True)

0 commit comments

Comments
 (0)