diff options
-rw-r--r-- | tests/coveragetest.py | 25 | ||||
-rw-r--r-- | tests/test_plugins.py | 16 | ||||
-rw-r--r-- | tests/test_testing.py | 39 |
3 files changed, 69 insertions, 11 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 7db25de0..18524758 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -3,6 +3,7 @@ """Base test case class for coverage.py testing.""" +import contextlib import datetime import glob import os @@ -251,6 +252,30 @@ class CoverageTest( return cov + @contextlib.contextmanager + def assert_warnings(self, cov, warnings): + """A context manager to check that particular warnings happened in `cov`.""" + saved_warnings = [] + def capture_warning(msg): + """A fake implementation of Coverage._warn, to capture warnings.""" + saved_warnings.append(msg) + + original_warn = cov._warn + cov._warn = capture_warning + + try: + yield + except: + raise + else: + for warning_regex in warnings: + for saved in saved_warnings: + if re.search(warning_regex, saved): + break + else: + self.fail("Didn't find warning %r in %r" % (warning_regex, saved_warnings)) + cov._warn = original_warn + def nice_file(self, *fparts): """Canonicalize the file name composed of the parts in `fparts`.""" fname = os.path.join(*fparts) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 06e3788d..54f26a7f 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -255,17 +255,11 @@ class PluginWarningOnPyTracer(CoverageTest): cov = coverage.Coverage() cov.set_option("run:plugins", ["tests.plugin1"]) - warnings = [] - def capture_warning(msg): - """A fake implementation of Coverage._warn, to capture warnings.""" - warnings.append(msg) - cov._warn = capture_warning - - self.start_import_stop(cov, "simple") - self.assertIn( - "Plugin file tracers (tests.plugin1.Plugin) aren't supported with PyTracer", - warnings - ) + expected_warnings = [ + r"Plugin file tracers \(tests.plugin1.Plugin\) aren't supported with PyTracer", + ] + with self.assert_warnings(cov, expected_warnings): + self.start_import_stop(cov, "simple") class FileTracerTest(CoverageTest): 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' |