@@ -128,7 +128,7 @@ is performed.
128
128
129
129
130
130
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,
132
132
allowing you to have finer control of which warnings should be captured at test, class or
133
133
even module level:
134
134
@@ -147,10 +147,30 @@ even module level:
147
147
assert api_v1() == 1
148
148
149
149
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
+
150
170
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.
152
172
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
154
174
decorator or to all tests in a module by setting the :globalvar: `pytestmark ` variable:
155
175
156
176
.. code-block :: python
@@ -159,6 +179,13 @@ decorator or to all tests in a module by setting the :globalvar:`pytestmark` var
159
179
pytestmark = pytest.mark.filterwarnings(" error" )
160
180
161
181
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
+
162
189
163
190
*Credits go to Florian Schulze for the reference implementation in the * `pytest-warnings `_
164
191
*plugin. *
0 commit comments