Skip to content

Add docs on pytest.warns(None) deprecation #9495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2022
Merged
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/9404.doc.rst
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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