From d8ecca5ebd929f1be49033f8a06eed97494af3da Mon Sep 17 00:00:00 2001 From: Joan Massich Date: Tue, 22 Aug 2017 13:48:29 +0200 Subject: [PATCH] Add test to design warns signature in TDD mimicking raises signature --- testing/test_recwarn.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 6895b1140..e4e7934cc 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -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)