@@ -42,6 +42,8 @@ Running pytest now produces this output:
42
42
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
43
43
======================= 1 passed, 1 warning in 0.12s =======================
44
44
45
+ .. _`controlling-warnings` :
46
+
45
47
Controlling warnings
46
48
--------------------
47
49
@@ -176,11 +178,14 @@ using an external system.
176
178
DeprecationWarning and PendingDeprecationWarning
177
179
------------------------------------------------
178
180
179
-
180
181
By default pytest will display ``DeprecationWarning `` and ``PendingDeprecationWarning `` warnings from
181
182
user code and third-party libraries, as recommended by :pep: `565 `.
182
183
This helps users keep their code modern and avoid breakages when deprecated warnings are effectively removed.
183
184
185
+ However, in the specific case where users capture any type of warnings in their test, either with
186
+ :func: `pytest.warns `, :func: `pytest.deprecated_call ` or using the :ref: `recwarn <recwarn >` fixture,
187
+ no warning will be displayed at all.
188
+
184
189
Sometimes it is useful to hide some specific deprecation warnings that happen in code that you have no control over
185
190
(such as third-party libraries), in which case you might use the warning filters options (ini or marks) to ignore
186
191
those warnings.
@@ -197,6 +202,9 @@ For example:
197
202
This will ignore all warnings of type ``DeprecationWarning `` where the start of the message matches
198
203
the regular expression ``".*U.*mode is deprecated" ``.
199
204
205
+ See :ref: `@pytest.mark.filterwarnings <filterwarnings >` and
206
+ :ref: `Controlling warnings <controlling-warnings >` for more examples.
207
+
200
208
.. note ::
201
209
202
210
If warnings are configured at the interpreter level, using
@@ -245,10 +253,10 @@ when called with a ``17`` argument.
245
253
Asserting warnings with the warns function
246
254
------------------------------------------
247
255
248
-
249
-
250
256
You can check that code raises a particular warning using :func: `pytest.warns `,
251
- which works in a similar manner to :ref: `raises <assertraises >`:
257
+ which works in a similar manner to :ref: `raises <assertraises >` (except that
258
+ :ref: `raises <assertraises >` does not capture all exceptions, only the
259
+ ``expected_exception ``):
252
260
253
261
.. code-block :: python
254
262
@@ -261,8 +269,8 @@ which works in a similar manner to :ref:`raises <assertraises>`:
261
269
with pytest.warns(UserWarning ):
262
270
warnings.warn(" my warning" , UserWarning )
263
271
264
- The test will fail if the warning in question is not raised. The keyword
265
- argument ``match `` to assert that the exception matches a text or regex::
272
+ The test will fail if the warning in question is not raised. Use the keyword
273
+ argument ``match `` to assert that the warning matches a text or regex::
266
274
267
275
>>> with warns(UserWarning, match='must be 0 or None'):
268
276
... warnings.warn("value must be 0 or None", UserWarning)
@@ -359,20 +367,32 @@ Additional use cases of warnings in tests
359
367
360
368
Here are some use cases involving warnings that often come up in tests, and suggestions on how to deal with them:
361
369
362
- - To ensure that **at least one ** warning is emitted , use:
370
+ - To ensure that **at least one ** of the indicated warnings is issued , use:
363
371
364
372
.. code-block :: python
365
373
366
- with pytest.warns():
374
+ def test_warning ():
375
+ with pytest.warns((RuntimeWarning , UserWarning )):
376
+ ...
377
+
378
+ - To ensure that **only ** certain warnings are issued, use:
379
+
380
+ .. code-block :: python
381
+
382
+ def test_warning (recwarn ):
367
383
...
384
+ assert len (recwarn) == 1
385
+ user_warning = recwarn.pop(UserWarning )
386
+ assert issubclass (user_warning.category, UserWarning )
368
387
369
388
- To ensure that **no ** warnings are emitted, use:
370
389
371
390
.. code-block :: python
372
391
373
- with warnings.catch_warnings():
374
- warnings.simplefilter(" error" )
375
- ...
392
+ def test_warning ():
393
+ with warnings.catch_warnings():
394
+ warnings.simplefilter(" error" )
395
+ ...
376
396
377
397
- To suppress warnings, use:
378
398
0 commit comments