Skip to content

Commit 2ad1b58

Browse files
authored
Add docs on pytest.warns(None) deprecation (#9495)
* Add docs on pytest.warns(None) deprecation * Add new section for common warnings use cases * Fix references for warnings use cases * Fix reference link
1 parent e9ed482 commit 2ad1b58

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

changelog/9404.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added extra documentation on alternatives to common misuses of `pytest.warns(None)` ahead of its deprecation.

doc/en/deprecations.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ Using ``pytest.warns(None)``
221221

222222
.. deprecated:: 7.0
223223

224-
:func:`pytest.warns(None) <pytest.warns>` is now deprecated because many people used
225-
it to mean "this code does not emit warnings", but it actually had the effect of
226-
checking that the code emits at least one warning of any type - like ``pytest.warns()``
224+
:func:`pytest.warns(None) <pytest.warns>` is now deprecated because it was frequently misused.
225+
Its correct usage was checking that the code emits at least one warning of any type - like ``pytest.warns()``
227226
or ``pytest.warns(Warning)``.
228227

228+
See :ref:`warns use cases` for examples.
229229

230230
The ``--strict`` command-line option
231231
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,35 @@ warnings, or index into it to get a particular recorded warning.
351351

352352
Full API: :class:`~_pytest.recwarn.WarningsRecorder`.
353353

354+
.. _`warns use cases`:
355+
356+
Additional use cases of warnings in tests
357+
-----------------------------------------
358+
359+
Here are some use cases involving warnings that often come up in tests, and suggestions on how to deal with them:
360+
361+
- To ensure that **any** warning is emitted, use:
362+
363+
.. code-block:: python
364+
365+
with pytest.warns():
366+
pass
367+
368+
- To ensure that **no** warnings are emitted, use:
369+
370+
.. code-block:: python
371+
372+
with warnings.catch_warnings():
373+
warnings.simplefilter("error")
374+
375+
- To suppress warnings, use:
376+
377+
.. code-block:: python
378+
379+
with warnings.catch_warnings():
380+
warnings.simplefilter("ignore")
381+
382+
354383
.. _custom_failure_messages:
355384

356385
Custom failure messages

src/_pytest/deprecated.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@
8888
)
8989

9090
WARNS_NONE_ARG = PytestRemovedIn8Warning(
91-
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n"
92-
" Replace pytest.warns(None) by simply pytest.warns()."
91+
"Passing None has been deprecated.\n"
92+
"See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
93+
"#additional-use-cases-of-warnings-in-tests"
94+
" for alternatives in common use cases."
9395
)
9496

9597
KEYWORD_MSG_ARG = UnformattedWarning(

testing/deprecated_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ def test_warns_none_is_deprecated():
138138
with pytest.warns(
139139
PytestDeprecationWarning,
140140
match=re.escape(
141-
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n "
142-
"Replace pytest.warns(None) by simply pytest.warns()."
141+
"Passing None has been deprecated.\n"
142+
"See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
143+
"#additional-use-cases-of-warnings-in-tests"
144+
" for alternatives in common use cases."
143145
),
144146
):
145147
with pytest.warns(None): # type: ignore[call-overload]

0 commit comments

Comments
 (0)