@@ -203,19 +203,21 @@ def test_deprecated_call_specificity(self) -> None:
203
203
def f ():
204
204
warnings .warn (warning ("hi" ))
205
205
206
- with pytest .raises (pytest .fail .Exception ):
207
- pytest .deprecated_call (f )
208
- with pytest .raises (pytest .fail .Exception ):
209
- with pytest .deprecated_call ():
210
- f ()
206
+ with pytest .warns (warning ):
207
+ with pytest .raises (pytest .fail .Exception ):
208
+ pytest .deprecated_call (f )
209
+ with pytest .raises (pytest .fail .Exception ):
210
+ with pytest .deprecated_call ():
211
+ f ()
211
212
212
213
def test_deprecated_call_supports_match (self ) -> None :
213
214
with pytest .deprecated_call (match = r"must be \d+$" ):
214
215
warnings .warn ("value must be 42" , DeprecationWarning )
215
216
216
- with pytest .raises (pytest .fail .Exception ):
217
- with pytest .deprecated_call (match = r"must be \d+$" ):
218
- warnings .warn ("this is not here" , DeprecationWarning )
217
+ with pytest .deprecated_call ():
218
+ with pytest .raises (pytest .fail .Exception , match = "DID NOT WARN" ):
219
+ with pytest .deprecated_call (match = r"must be \d+$" ):
220
+ warnings .warn ("this is not here" , DeprecationWarning )
219
221
220
222
221
223
class TestWarns :
@@ -227,8 +229,9 @@ def test_check_callable(self) -> None:
227
229
def test_several_messages (self ) -> None :
228
230
# different messages, b/c Python suppresses multiple identical warnings
229
231
pytest .warns (RuntimeWarning , lambda : warnings .warn ("w1" , RuntimeWarning ))
230
- with pytest .raises (pytest .fail .Exception ):
231
- pytest .warns (UserWarning , lambda : warnings .warn ("w2" , RuntimeWarning ))
232
+ with pytest .warns (RuntimeWarning ):
233
+ with pytest .raises (pytest .fail .Exception ):
234
+ pytest .warns (UserWarning , lambda : warnings .warn ("w2" , RuntimeWarning ))
232
235
pytest .warns (RuntimeWarning , lambda : warnings .warn ("w3" , RuntimeWarning ))
233
236
234
237
def test_function (self ) -> None :
@@ -243,13 +246,14 @@ def test_warning_tuple(self) -> None:
243
246
pytest .warns (
244
247
(RuntimeWarning , SyntaxWarning ), lambda : warnings .warn ("w2" , SyntaxWarning )
245
248
)
246
- pytest .raises (
247
- pytest .fail .Exception ,
248
- lambda : pytest .warns (
249
- (RuntimeWarning , SyntaxWarning ),
250
- lambda : warnings .warn ("w3" , UserWarning ),
251
- ),
252
- )
249
+ with pytest .warns ():
250
+ pytest .raises (
251
+ pytest .fail .Exception ,
252
+ lambda : pytest .warns (
253
+ (RuntimeWarning , SyntaxWarning ),
254
+ lambda : warnings .warn ("w3" , UserWarning ),
255
+ ),
256
+ )
253
257
254
258
def test_as_contextmanager (self ) -> None :
255
259
with pytest .warns (RuntimeWarning ):
@@ -258,40 +262,43 @@ def test_as_contextmanager(self) -> None:
258
262
with pytest .warns (UserWarning ):
259
263
warnings .warn ("user" , UserWarning )
260
264
261
- with pytest .raises (pytest .fail .Exception ) as excinfo :
262
- with pytest .warns (RuntimeWarning ):
263
- warnings .warn ("user" , UserWarning )
265
+ with pytest .warns ():
266
+ with pytest .raises (pytest .fail .Exception ) as excinfo :
267
+ with pytest .warns (RuntimeWarning ):
268
+ warnings .warn ("user" , UserWarning )
264
269
excinfo .match (
265
270
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted.\n"
266
- r"The list of emitted warnings is : \[UserWarning\('user',?\)\]."
271
+ r" Emitted warnings: \[UserWarning\('user',?\)\]."
267
272
)
268
273
269
- with pytest .raises (pytest .fail .Exception ) as excinfo :
270
- with pytest .warns (UserWarning ):
271
- warnings .warn ("runtime" , RuntimeWarning )
274
+ with pytest .warns ():
275
+ with pytest .raises (pytest .fail .Exception ) as excinfo :
276
+ with pytest .warns (UserWarning ):
277
+ warnings .warn ("runtime" , RuntimeWarning )
272
278
excinfo .match (
273
279
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
274
- r"The list of emitted warnings is : \[RuntimeWarning\('runtime',?\)]."
280
+ r" Emitted warnings: \[RuntimeWarning\('runtime',?\)]."
275
281
)
276
282
277
283
with pytest .raises (pytest .fail .Exception ) as excinfo :
278
284
with pytest .warns (UserWarning ):
279
285
pass
280
286
excinfo .match (
281
287
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
282
- r"The list of emitted warnings is : \[\]."
288
+ r" Emitted warnings: \[\]."
283
289
)
284
290
285
291
warning_classes = (UserWarning , FutureWarning )
286
- with pytest .raises (pytest .fail .Exception ) as excinfo :
287
- with pytest .warns (warning_classes ) as warninfo :
288
- warnings .warn ("runtime" , RuntimeWarning )
289
- warnings .warn ("import" , ImportWarning )
292
+ with pytest .warns ():
293
+ with pytest .raises (pytest .fail .Exception ) as excinfo :
294
+ with pytest .warns (warning_classes ) as warninfo :
295
+ warnings .warn ("runtime" , RuntimeWarning )
296
+ warnings .warn ("import" , ImportWarning )
290
297
291
298
messages = [each .message for each in warninfo ]
292
299
expected_str = (
293
300
f"DID NOT WARN. No warnings of type { warning_classes } were emitted.\n "
294
- f"The list of emitted warnings is : { messages } ."
301
+ f" Emitted warnings: { messages } ."
295
302
)
296
303
297
304
assert str (excinfo .value ) == expected_str
@@ -367,25 +374,31 @@ def test_match_regex(self) -> None:
367
374
with pytest .warns (UserWarning , match = r"must be \d+$" ):
368
375
warnings .warn ("value must be 42" , UserWarning )
369
376
370
- with pytest .raises (pytest .fail .Exception ):
371
- with pytest .warns (UserWarning , match = r"must be \d+$" ):
372
- warnings .warn ("this is not here" , UserWarning )
377
+ with pytest .warns ():
378
+ with pytest .raises (pytest .fail .Exception ):
379
+ with pytest .warns (UserWarning , match = r"must be \d+$" ):
380
+ warnings .warn ("this is not here" , UserWarning )
373
381
374
- with pytest .raises (pytest .fail .Exception ):
375
- with pytest .warns (FutureWarning , match = r"must be \d+$" ):
376
- warnings .warn ("value must be 42" , UserWarning )
382
+ with pytest .warns ():
383
+ with pytest .raises (pytest .fail .Exception ):
384
+ with pytest .warns (FutureWarning , match = r"must be \d+$" ):
385
+ warnings .warn ("value must be 42" , UserWarning )
377
386
378
387
def test_one_from_multiple_warns (self ) -> None :
379
- with pytest .warns (UserWarning , match = r"aaa" ):
380
- warnings .warn ("cccccccccc" , UserWarning )
381
- warnings .warn ("bbbbbbbbbb" , UserWarning )
382
- warnings .warn ("aaaaaaaaaa" , UserWarning )
388
+ with pytest .warns ():
389
+ with pytest .raises (pytest .fail .Exception , match = "DID NOT WARN" ):
390
+ with pytest .warns (UserWarning , match = r"aaa" ):
391
+ with pytest .warns (UserWarning , match = r"aaa" ):
392
+ warnings .warn ("cccccccccc" , UserWarning )
393
+ warnings .warn ("bbbbbbbbbb" , UserWarning )
394
+ warnings .warn ("aaaaaaaaaa" , UserWarning )
383
395
384
396
def test_none_of_multiple_warns (self ) -> None :
385
- with pytest .raises (pytest .fail .Exception ):
386
- with pytest .warns (UserWarning , match = r"aaa" ):
387
- warnings .warn ("bbbbbbbbbb" , UserWarning )
388
- warnings .warn ("cccccccccc" , UserWarning )
397
+ with pytest .warns ():
398
+ with pytest .raises (pytest .fail .Exception , match = "DID NOT WARN" ):
399
+ with pytest .warns (UserWarning , match = r"aaa" ):
400
+ warnings .warn ("bbbbbbbbbb" , UserWarning )
401
+ warnings .warn ("cccccccccc" , UserWarning )
389
402
390
403
@pytest .mark .filterwarnings ("ignore" )
391
404
def test_can_capture_previously_warned (self ) -> None :
@@ -403,3 +416,33 @@ def test_warns_context_manager_with_kwargs(self) -> None:
403
416
with pytest .warns (UserWarning , foo = "bar" ): # type: ignore
404
417
pass
405
418
assert "Unexpected keyword arguments" in str (excinfo .value )
419
+
420
+ def test_re_emit_single (self ) -> None :
421
+ with pytest .warns (DeprecationWarning ):
422
+ with pytest .warns (UserWarning ):
423
+ warnings .warn ("user warning" , UserWarning )
424
+ warnings .warn ("some deprecation warning" , DeprecationWarning )
425
+
426
+ def test_re_emit_multiple (self ) -> None :
427
+ with pytest .warns (UserWarning ):
428
+ warnings .warn ("first warning" , UserWarning )
429
+ warnings .warn ("second warning" , UserWarning )
430
+
431
+ def test_re_emit_match_single (self ) -> None :
432
+ with pytest .warns (DeprecationWarning ):
433
+ with pytest .warns (UserWarning , match = "user warning" ):
434
+ warnings .warn ("user warning" , UserWarning )
435
+ warnings .warn ("some deprecation warning" , DeprecationWarning )
436
+
437
+ def test_re_emit_match_multiple (self ) -> None :
438
+ with warnings .catch_warnings ():
439
+ warnings .simplefilter ("error" ) # if anything is re-emitted
440
+ with pytest .warns (UserWarning , match = "user warning" ):
441
+ warnings .warn ("first user warning" , UserWarning )
442
+ warnings .warn ("second user warning" , UserWarning )
443
+
444
+ def test_re_emit_non_match_single (self ) -> None :
445
+ with pytest .warns (UserWarning , match = "v2 warning" ):
446
+ with pytest .warns (UserWarning , match = "v1 warning" ):
447
+ warnings .warn ("v1 warning" , UserWarning )
448
+ warnings .warn ("non-matching v2 warning" , UserWarning )
0 commit comments