diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-10-03 16:34:52 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-10-03 16:34:52 -0400 |
commit | b27f9eab849696b46e9fbb84dcdc0f34078684d1 (patch) | |
tree | e55aacebb0b88fa430cf3b174f8eac041fbd4ef3 /tests/test_testing.py | |
parent | 9a3c3e8b65070b31daf31cc149f28241cead89bf (diff) | |
download | python-coveragepy-git-b27f9eab849696b46e9fbb84dcdc0f34078684d1.tar.gz |
A better way to test for warnings.
Diffstat (limited to 'tests/test_testing.py')
-rw-r--r-- | tests/test_testing.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/test_testing.py b/tests/test_testing.py index 54ab894d..9fc7f11d 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -8,6 +8,7 @@ import datetime import os import sys +import coverage from coverage.backunittest import TestCase from coverage.backward import to_bytes from coverage.files import actual_path @@ -146,6 +147,44 @@ class CoverageTestTest(CoverageTest): with self.assertRaises(AssertionError): self.assert_recent_datetime(now_delta(1), seconds=120) + def test_assert_warnings(self): + cov = coverage.Coverage() + + # Make a warning, it should catch it properly. + with self.assert_warnings(cov, ["Hello there!"]): + cov._warn("Hello there!") + + # The expected warnings are regexes. + with self.assert_warnings(cov, ["Hello.*!"]): + cov._warn("Hello there!") + + # There can be a bunch of actual warnings. + with self.assert_warnings(cov, ["Hello.*!"]): + cov._warn("You there?") + cov._warn("Hello there!") + + # There can be a bunch of expected warnings. + with self.assert_warnings(cov, ["Hello.*!", "You"]): + cov._warn("You there?") + cov._warn("Hello there!") + + # But if there are a bunch of expected warnings, they have to all happen. + warn_regex = r"Didn't find warning 'You' in \['Hello there!'\]" + with self.assertRaisesRegex(AssertionError, warn_regex): + with self.assert_warnings(cov, ["Hello.*!", "You"]): + cov._warn("Hello there!") + + # Make a different warning than expected, it should raise an assertion. + warn_regex = r"Didn't find warning 'Not me' in \['Hello there!'\]" + with self.assertRaisesRegex(AssertionError, warn_regex): + with self.assert_warnings(cov, ["Not me"]): + cov._warn("Hello there!") + + # assert_warnings shouldn't hide a real exception. + with self.assertRaises(ZeroDivisionError): + with self.assert_warnings(cov, ["Hello there!"]): + raise ZeroDivisionError("oops") + def test_sub_python_is_this_python(self): # Try it with a Python command. os.environ['COV_FOOBAR'] = 'XYZZY' |