Update capture-warnings.rst doc on syntax

Re-order and clarify that the syntax format of pytest is the exact same as python's -W syntax
This commit is contained in:
Dan Alvizu 2021-12-30 15:43:12 -07:00 committed by GitHub
parent 9a992df3c9
commit 4995bc30af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 4 deletions

View File

@ -42,8 +42,18 @@ Running pytest now produces this output:
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================= 1 passed, 1 warning in 0.12s ======================= ======================= 1 passed, 1 warning in 0.12s =======================
The ``-W`` flag can be passed to control which warnings will be displayed or even turn Controlling warnings
them into errors: --------------------
Similar to Python's `warning filter` and :option:`-W option <python:-W>` flag, pytest provides
its own a ``-W`` flag to control which warnings are ignored, displayed, or turned into
errors. See the `warning filter` documentation for more
advanced use-cases.
.. _`warning filter`: https://docs.python.org/3/library/warnings.html#warning-filter
This code sample shows how to treat any ``UserWarning`` category class of warning
as an error:
.. code-block:: pytest .. code-block:: pytest
@ -96,9 +106,11 @@ all other warnings into errors.
When a warning matches more than one option in the list, the action for the last matching option When a warning matches more than one option in the list, the action for the last matching option
is performed. is performed.
Syntax
------
Both ``-W`` command-line option and ``filterwarnings`` ini option are based on Python's own Both ``-W`` command-line option and ``filterwarnings`` ini option are based on Python's own
:option:`-W option <python:-W>` and :func:`warnings.simplefilter`, so please refer to those sections in the Python
documentation for other examples and advanced usage.
.. _`filterwarnings`: .. _`filterwarnings`: