Add test to design warns signature in TDD mimicking raises signature

This commit is contained in:
Joan Massich 2017-08-22 13:48:29 +02:00
parent 5c0feb2877
commit d8ecca5ebd
1 changed files with 22 additions and 0 deletions

View File

@ -284,3 +284,25 @@ class TestWarns(object):
''')
result = testdir.runpytest()
result.stdout.fnmatch_lines(['*2 passed in*'])
def test_match_regex(self):
with pytest.warns(UserWarning, match=r'must be \d+$'):
warnings.warn("value must be 42", UserWarning)
with pytest.raises(AssertionError, match='pattern not found'):
with pytest.warns(UserWarning, match=r'must be \d+$'):
warnings.warn("this is not here", UserWarning)
def test_one_from_multiple_warns():
with warns(UserWarning, match=r'aaa'):
warnings.warn("cccccccccc", UserWarning)
warnings.warn("bbbbbbbbbb", UserWarning)
warnings.warn("aaaaaaaaaa", UserWarning)
def test_none_of_multiple_warns():
a, b, c = ('aaa', 'bbbbbbbbbb', 'cccccccccc')
expected_msg = "'{}' pattern not found in \['{}', '{}'\]".format(a, b, c)
with raises(AssertionError, match=expected_msg):
with warns(UserWarning, match=r'aaa'):
warnings.warn("bbbbbbbbbb", UserWarning)
warnings.warn("cccccccccc", UserWarning)