From 51ea042162e46ecf7bba975ba2a5c89da04d11a9 Mon Sep 17 00:00:00 2001 From: Olga Matoula Date: Thu, 13 Jan 2022 18:06:03 +0000 Subject: [PATCH] Fix references for warnings use cases --- doc/en/deprecations.rst | 2 +- doc/en/how-to/capture-warnings.rst | 55 ++++++++++++++++-------------- src/_pytest/deprecated.py | 2 +- testing/deprecated_test.py | 2 +- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index 358967fae..0851341ba 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -225,7 +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)``. -See https://docs.pytest.org/en/latest/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests for use cases. +See :ref:`warns-use-cases` for examples. The ``--strict`` command-line option ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/en/how-to/capture-warnings.rst b/doc/en/how-to/capture-warnings.rst index 5ae59b7f8..81f2ebd6e 100644 --- a/doc/en/how-to/capture-warnings.rst +++ b/doc/en/how-to/capture-warnings.rst @@ -300,32 +300,6 @@ 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 ------------------ @@ -370,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 diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 55a09d496..4534fbcab 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -91,7 +91,7 @@ WARNS_NONE_ARG = PytestRemovedIn8Warning( "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." + " for alternatives in common use cases." ) KEYWORD_MSG_ARG = UnformattedWarning( diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 2bebfe25f..a567201b2 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -141,7 +141,7 @@ def test_warns_none_is_deprecated(): "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." + " for alternatives in common use cases." ), ): with pytest.warns(None): # type: ignore[call-overload]