Add new section for common warnings use cases
This commit is contained in:
parent
aeae183c75
commit
e69f04e3ce
|
@ -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()``
|
||||
or ``pytest.warns(Warning)``.
|
||||
|
||||
If you are looking to:
|
||||
|
||||
- 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
|
||||
See https://docs.pytest.org/en/latest/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests for use cases.
|
||||
|
||||
The ``--strict`` command-line option
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
@ -300,6 +300,32 @@ filter at the end of the test, so no global state is leaked.
|
|||
|
||||
.. _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
|
||||
------------------
|
||||
|
||||
|
|
|
@ -88,11 +88,10 @@ NODE_CTOR_FSPATH_ARG = UnformattedWarning(
|
|||
)
|
||||
|
||||
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().\n"
|
||||
"See https://docs.pytest.org/en/latest/deprecations.html"
|
||||
"#using-pytest-warns-none"
|
||||
" on asserting no warnings were emitted."
|
||||
"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 alternativesin common use cases."
|
||||
)
|
||||
|
||||
KEYWORD_MSG_ARG = UnformattedWarning(
|
||||
|
|
|
@ -138,11 +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().\n"
|
||||
"See https://docs.pytest.org/en/latest/deprecations.html"
|
||||
"#using-pytest-warns-none"
|
||||
" on asserting no warnings were emitted."
|
||||
"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 alternativesin common use cases."
|
||||
),
|
||||
):
|
||||
with pytest.warns(None): # type: ignore[call-overload]
|
||||
|
|
Loading…
Reference in New Issue