Skip to content

Commit

Permalink
Add docs on pytest.warns(None) deprecation (#9495)
Browse files Browse the repository at this point in the history
* Add docs on pytest.warns(None) deprecation

* Add new section for common warnings use cases

* Fix references for warnings use cases

* Fix reference link
  • Loading branch information
olgarithms committed Jan 13, 2022
1 parent e9ed482 commit 2ad1b58
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/9404.doc.rst
@@ -0,0 +1 @@
Added extra documentation on alternatives to common misuses of `pytest.warns(None)` ahead of its deprecation.
6 changes: 3 additions & 3 deletions doc/en/deprecations.rst
Expand Up @@ -221,11 +221,11 @@ Using ``pytest.warns(None)``

.. deprecated:: 7.0

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

See :ref:`warns use cases` for examples.

The ``--strict`` command-line option
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
29 changes: 29 additions & 0 deletions doc/en/how-to/capture-warnings.rst
Expand Up @@ -351,6 +351,35 @@ warnings, or index into it to get a particular recorded warning.

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

.. _`warns use cases`:

Additional use cases of warnings in tests
-----------------------------------------

Here are some use cases involving warnings that often come up in tests, and suggestions on how to deal with them:

- To ensure that **any** warning is emitted, use:

.. code-block:: python
with pytest.warns():
pass
- To ensure that **no** warnings are emitted, use:

.. code-block:: python
with warnings.catch_warnings():
warnings.simplefilter("error")
- To suppress warnings, use:

.. code-block:: python
with warnings.catch_warnings():
warnings.simplefilter("ignore")
.. _custom_failure_messages:

Custom failure messages
Expand Down
6 changes: 4 additions & 2 deletions src/_pytest/deprecated.py
Expand Up @@ -88,8 +88,10 @@
)

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

KEYWORD_MSG_ARG = UnformattedWarning(
Expand Down
6 changes: 4 additions & 2 deletions testing/deprecated_test.py
Expand Up @@ -138,8 +138,10 @@ def test_warns_none_is_deprecated():
with pytest.warns(
PytestDeprecationWarning,
match=re.escape(
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n "
"Replace pytest.warns(None) by simply pytest.warns()."
"Passing None has been deprecated.\n"
"See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
"#additional-use-cases-of-warnings-in-tests"
" for alternatives in common use cases."
),
):
with pytest.warns(None): # type: ignore[call-overload]
Expand Down

0 comments on commit 2ad1b58

Please sign in to comment.