@@ -284,3 +284,29 @@ def test(run):
284
284
''' )
285
285
result = testdir .runpytest ()
286
286
result .stdout .fnmatch_lines (['*2 passed in*' ])
287
+
288
+ def test_match_regex (self ):
289
+ source = "warnings.warn('value must be 42', UserWarning)"
290
+ pytest .warns (UserWarning , source , match = r'must be \d+$' )
291
+
292
+ def test_match_regex_using_contextmanager (self ):
293
+ with pytest .warns (UserWarning , match = r'must be \d+$' ):
294
+ warnings .warn ("value must be 42" , UserWarning )
295
+
296
+ with pytest .raises (AssertionError , match = 'pattern not found' ):
297
+ with pytest .warns (UserWarning , match = r'must be \d+$' ):
298
+ warnings .warn ("this is not here" , UserWarning )
299
+
300
+ def test_one_from_multiple_warns ():
301
+ with warns (UserWarning , match = r'aaa' ):
302
+ warnings .warn ("cccccccccc" , UserWarning )
303
+ warnings .warn ("bbbbbbbbbb" , UserWarning )
304
+ warnings .warn ("aaaaaaaaaa" , UserWarning )
305
+
306
+ def test_none_of_multiple_warns ():
307
+ a , b , c = ('aaa' , 'bbbbbbbbbb' , 'cccccccccc' )
308
+ expected_msg = "'{}' pattern not found in \['{}', '{}'\]" .format (a , b , c )
309
+ with raises (AssertionError , match = expected_msg ):
310
+ with warns (UserWarning , match = r'aaa' ):
311
+ warnings .warn ("bbbbbbbbbb" , UserWarning )
312
+ warnings .warn ("cccccccccc" , UserWarning )
0 commit comments