Skip to content

Commit be6bc81

Browse files
Issue #12966 Clarify filterwarnings docs on precedence when using multiple marks (#12967) (#12969)
(cherry picked from commit 71a35d4) Co-authored-by: Stefaan Lippens <soxofaan@users.noreply.github.com>
1 parent 7aeb72b commit be6bc81

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

changelog/12966.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clarify :ref:`filterwarnings` docs on filter precedence/order when using multiple :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` marks.

doc/en/how-to/capture-warnings.rst

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ is performed.
128128

129129

130130

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

@@ -147,10 +147,30 @@ even module level:
147147
assert api_v1() == 1
148148
149149
150+
You can specify multiple filters with separate decorators:
151+
152+
.. code-block:: python
153+
154+
# Ignore "api v1" warnings, but fail on all other warnings
155+
@pytest.mark.filterwarnings("ignore:api v1")
156+
@pytest.mark.filterwarnings("error")
157+
def test_one():
158+
assert api_v1() == 1
159+
160+
.. important::
161+
162+
Regarding decorator order and filter precedence:
163+
it's important to remember that decorators are evaluated in reverse order,
164+
so you have to list the warning filters in the reverse order
165+
compared to traditional :py:func:`warnings.filterwarnings` and :option:`-W option <python:-W>` usage.
166+
This means in practice that filters from earlier :ref:`@pytest.mark.filterwarnings <pytest.mark.filterwarnings ref>` decorators
167+
take precedence over filters from later decorators, as illustrated in the example above.
168+
169+
150170
Filters applied using a mark take precedence over filters passed on the command line or configured
151-
by the ``filterwarnings`` ini option.
171+
by the :confval:`filterwarnings` ini option.
152172

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

156176
.. code-block:: python
@@ -159,6 +179,13 @@ decorator or to all tests in a module by setting the :globalvar:`pytestmark` var
159179
pytestmark = pytest.mark.filterwarnings("error")
160180
161181
182+
.. note::
183+
184+
If you want to apply multiple filters
185+
(by assigning a list of :ref:`filterwarnings <pytest.mark.filterwarnings ref>` mark to :globalvar:`pytestmark`),
186+
you must use the traditional :py:func:`warnings.filterwarnings` ordering approach (later filters take precedence),
187+
which is the reverse of the decorator approach mentioned above.
188+
162189

163190
*Credits go to Florian Schulze for the reference implementation in the* `pytest-warnings`_
164191
*plugin.*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy