Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify docs on specifying multiple filters with pytest.mark.filterwarnings #12967

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/12966.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clarify :ref:`filterwarnings` docs on filter precedence/order when using multiple :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` marks.
33 changes: 30 additions & 3 deletions doc/en/how-to/capture-warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ is performed.



You can use the ``@pytest.mark.filterwarnings`` to add warning filters to specific test items,
You can use the :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` mark to add warning filters to specific test items,
allowing you to have finer control of which warnings should be captured at test, class or
even module level:

Expand All @@ -147,10 +147,30 @@ even module level:
assert api_v1() == 1


You can specify multiple filters with separate decorators:

.. code-block:: python

# Ignore "api v1" warnings, but fail on all other warnings
@pytest.mark.filterwarnings("ignore:api v1")
@pytest.mark.filterwarnings("error")
def test_one():
assert api_v1() == 1

.. important::

Regarding decorator order and filter precedence:
it's important to remember that decorators are evaluated in reverse order,
so you have to list the warning filters in the reverse order
compared to traditional :py:func:`warnings.filterwarnings` and :option:`-W option <python:-W>` usage.
This means in practice that filters from earlier :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` decorators
take precedence over filters from later decorators, as illustrated in the example above.


Filters applied using a mark take precedence over filters passed on the command line or configured
by the ``filterwarnings`` ini option.
by the :confval:`filterwarnings` ini option.

You may apply a filter to all tests of a class by using the ``filterwarnings`` mark as a class
You may apply a filter to all tests of a class by using the :ref:`filterwarnings <pytest.mark.filterwarnings ref>` mark as a class
decorator or to all tests in a module by setting the :globalvar:`pytestmark` variable:

.. code-block:: python
Expand All @@ -159,6 +179,13 @@ decorator or to all tests in a module by setting the :globalvar:`pytestmark` var
pytestmark = pytest.mark.filterwarnings("error")


.. note::

If you want to apply multiple filters
(by assigning a list of :ref:`filterwarnings <pytest.mark.filterwarnings ref>` mark to :globalvar:`pytestmark`),
you must use the traditional :py:func:`warnings.filterwarnings` ordering approach (later filters take precedence),
which is the reverse of the decorator approach mentioned above.


*Credits go to Florian Schulze for the reference implementation in the* `pytest-warnings`_
*plugin.*
Expand Down
Loading