@@ -284,3 +284,27 @@ def test(run):
284
284
''' )
285
285
result = testdir .runpytest ()
286
286
result .stdout .fnmatch_lines (['*2 passed in*' ])
287
+
288
+ def test_message_using (self ):
289
+ source = "warnings.warn('my runtime warning', RuntimeWarning)"
290
+ pytest .warns (RuntimeWarning , source , message = 'my runtime warning' )
291
+
292
+ def test_message_using_contextmanager (self ):
293
+ with pytest .warns (RuntimeWarning , message = 'my runtime warning' ):
294
+ warnings .warn ("my runtime warning" , RuntimeWarning )
295
+
296
+ with pytest .raises (AssertionError , match = 'is different from' ):
297
+ with pytest .warns (RuntimeWarning , message = "my runtime warning" ):
298
+ warnings .warn ("different message" , RuntimeWarning )
299
+
300
+ def test_match_regex (self ):
301
+ source = "warnings.warn('value must be 42', UserWarning)"
302
+ pytest .warns (UserWarning , source , match = r'must be \d+$' )
303
+
304
+ def test_match_regex_using_contextmanager (self ):
305
+ with pytest .warns (UserWarning , match = r'must be \d+$' ):
306
+ warnings .warn ("value must be 42" , UserWarning )
307
+
308
+ with pytest .raises (AssertionError , match = 'pattern not found' ):
309
+ with pytest .warns (UserWarning , match = r'must be \d+$' ):
310
+ warnings .warn ("this is not here" , UserWarning )
0 commit comments