@@ -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,17 +262,19 @@ 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
280
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)]."
@@ -283,10 +289,11 @@ def test_as_contextmanager(self) -> None:
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 = (
@@ -367,27 +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 .raises ( pytest . fail . Exception , match = "DID NOT WARN" ):
380
- with pytest .warns ( UserWarning , match = r"aaa " ):
388
+ with pytest .warns ( ):
389
+ with pytest .raises ( pytest . fail . Exception , match = "DID NOT WARN " ):
381
390
with pytest .warns (UserWarning , match = r"aaa" ):
382
- warnings .warn ("cccccccccc" , UserWarning )
383
- warnings .warn ("bbbbbbbbbb" , UserWarning )
384
- warnings .warn ("aaaaaaaaaa" , UserWarning )
391
+ with pytest .warns (UserWarning , match = r"aaa" ):
392
+ warnings .warn ("cccccccccc" , UserWarning )
393
+ warnings .warn ("bbbbbbbbbb" , UserWarning )
394
+ warnings .warn ("aaaaaaaaaa" , UserWarning )
385
395
386
396
def test_none_of_multiple_warns (self ) -> None :
387
- with pytest .raises (pytest .fail .Exception , match = "DID NOT WARN" ):
388
- with pytest .warns (UserWarning , match = r"aaa" ):
389
- warnings .warn ("bbbbbbbbbb" , UserWarning )
390
- 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 )
391
402
392
403
@pytest .mark .filterwarnings ("ignore" )
393
404
def test_can_capture_previously_warned (self ) -> None :
0 commit comments