Add new section for common warnings use cases

This commit is contained in:
Olga Matoula 2022-01-12 17:42:15 +00:00
parent aeae183c75
commit e69f04e3ce
4 changed files with 35 additions and 32 deletions

View File

@ -225,28 +225,7 @@ Using ``pytest.warns(None)``
Its correct usage was checking that the code emits at least one warning of any type - like ``pytest.warns()`` Its correct usage was checking that the code emits at least one warning of any type - like ``pytest.warns()``
or ``pytest.warns(Warning)``. or ``pytest.warns(Warning)``.
If you are looking to: See https://docs.pytest.org/en/latest/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests for use cases.
- ensure that **no** warnings are emitted, consider using
.. code-block:: python
with warnings.catch_warnings():
warnings.simplefilter("error")
- suppress warnings, you could use
.. code-block:: python
with warnings.catch_warnings():
warnings.simplefilter("ignore")
- ensure that **any** warning is emitted, please use
.. code-block:: python
with pytest.warns():
pass
The ``--strict`` command-line option The ``--strict`` command-line option
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -300,6 +300,32 @@ filter at the end of the test, so no global state is leaked.
.. _recwarn: .. _recwarn:
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")
Recording warnings Recording warnings
------------------ ------------------

View File

@ -88,11 +88,10 @@ NODE_CTOR_FSPATH_ARG = UnformattedWarning(
) )
WARNS_NONE_ARG = PytestRemovedIn8Warning( WARNS_NONE_ARG = PytestRemovedIn8Warning(
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n" "Passing None has been deprecated.\n"
"Replace pytest.warns(None) by simply pytest.warns().\n" "See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
"See https://docs.pytest.org/en/latest/deprecations.html" "#additional-use-cases-of-warnings-in-tests"
"#using-pytest-warns-none" " for alternativesin common use cases."
" on asserting no warnings were emitted."
) )
KEYWORD_MSG_ARG = UnformattedWarning( KEYWORD_MSG_ARG = UnformattedWarning(

View File

@ -138,11 +138,10 @@ def test_warns_none_is_deprecated():
with pytest.warns( with pytest.warns(
PytestDeprecationWarning, PytestDeprecationWarning,
match=re.escape( match=re.escape(
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n" "Passing None has been deprecated.\n"
"Replace pytest.warns(None) by simply pytest.warns().\n" "See https://docs.pytest.org/en/latest/how-to/capture-warnings.html"
"See https://docs.pytest.org/en/latest/deprecations.html" "#additional-use-cases-of-warnings-in-tests"
"#using-pytest-warns-none" " for alternativesin common use cases."
" on asserting no warnings were emitted."
), ),
): ):
with pytest.warns(None): # type: ignore[call-overload] with pytest.warns(None): # type: ignore[call-overload]