Skip to content

Commit 46c06e7

Browse files
authored
Merge pull request pytest-dev#9508 from pytest-dev/backport-9495-to-7.0.x
2 parents 4967a0d + 94bcd2c commit 46c06e7

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
@@ -115,8 +115,10 @@
115115
)
116116

117117
WARNS_NONE_ARG = PytestRemovedIn8Warning(
118-
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n"
119-
" Replace pytest.warns(None) by simply pytest.warns()."
118+
"Passing None has been deprecated.\n"
119+
"See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
120+
"#additional-use-cases-of-warnings-in-tests"
121+
" for alternatives in common use cases."
120122
)
121123

122124
KEYWORD_MSG_ARG = UnformattedWarning(

testing/deprecated_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ def test_warns_none_is_deprecated():
194194
with pytest.warns(
195195
PytestDeprecationWarning,
196196
match=re.escape(
197-
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n "
198-
"Replace pytest.warns(None) by simply pytest.warns()."
197+
"Passing None has been deprecated.\n"
198+
"See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
199+
"#additional-use-cases-of-warnings-in-tests"
200+
" for alternatives in common use cases."
199201
),
200202
):
201203
with pytest.warns(None): # type: ignore[call-overload]

0 commit comments

Comments
 (0)