Skip to content

Commit 47e17cb

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

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

testing/test_recwarn.py

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

Comments
 (0)