Skip to content

Commit 44e4e2a

Browse files
author
Joan Massich
committed
Add test to design warns signature in TDD mimicking raises signature
1 parent 5c0feb2 commit 44e4e2a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

testing/test_recwarn.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,27 @@ def test(run):
284284
''')
285285
result = testdir.runpytest()
286286
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

Comments
 (0)