Add docs on pytest.warns(None) deprecation (#9495)
* Add docs on pytest.warns(None) deprecation * Add new section for common warnings use cases * Fix references for warnings use cases * Fix reference link
This commit is contained in:
parent
e9ed4827a4
commit
2ad1b589af
|
@ -0,0 +1 @@
|
||||||
|
Added extra documentation on alternatives to common misuses of `pytest.warns(None)` ahead of its deprecation.
|
|
@ -221,11 +221,11 @@ Using ``pytest.warns(None)``
|
||||||
|
|
||||||
.. deprecated:: 7.0
|
.. deprecated:: 7.0
|
||||||
|
|
||||||
:func:`pytest.warns(None) <pytest.warns>` is now deprecated because many people used
|
:func:`pytest.warns(None) <pytest.warns>` is now deprecated because it was frequently misused.
|
||||||
it to mean "this code does not emit warnings", but it actually had the effect of
|
Its correct usage was checking that the code emits at least one warning of any type - like ``pytest.warns()``
|
||||||
checking that the code emits at least one warning of any type - like ``pytest.warns()``
|
|
||||||
or ``pytest.warns(Warning)``.
|
or ``pytest.warns(Warning)``.
|
||||||
|
|
||||||
|
See :ref:`warns use cases` for examples.
|
||||||
|
|
||||||
The ``--strict`` command-line option
|
The ``--strict`` command-line option
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
|
@ -351,6 +351,35 @@ warnings, or index into it to get a particular recorded warning.
|
||||||
|
|
||||||
Full API: :class:`~_pytest.recwarn.WarningsRecorder`.
|
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:
|
||||||
|
|
||||||
Custom failure messages
|
Custom failure messages
|
||||||
|
|
|
@ -88,8 +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()."
|
"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(
|
KEYWORD_MSG_ARG = UnformattedWarning(
|
||||||
|
|
|
@ -138,8 +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()."
|
"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]
|
with pytest.warns(None): # type: ignore[call-overload]
|
||||||
|
|
Loading…
Reference in New Issue