summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-02-20 14:22:06 -0500
committerNed Batchelder <ned@nedbatchelder.com>2017-02-20 14:22:06 -0500
commit66af7e3d24084829850b8bb6d671ddb0094a0331 (patch)
tree5eccacaa10f35524c24f958f959e8a57547d2eb7
parentc59233e638cc632afc80251463ae9f391beea365 (diff)
downloadpython-coveragepy-git-66af7e3d24084829850b8bb6d671ddb0094a0331.tar.gz
assert_warnings can now assert that there were no warnings.
-rw-r--r--tests/coveragetest.py29
-rw-r--r--tests/test_testing.py13
2 files changed, 35 insertions, 7 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index 4df947d5..cacd4362 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -270,7 +270,16 @@ class CoverageTest(
@contextlib.contextmanager
def assert_warnings(self, cov, warnings):
- """A context manager to check that particular warnings happened in `cov`."""
+ """A context manager to check that particular warnings happened in `cov`.
+
+ `cov` is a Coverage instance. `warnings` is a list of regexes. Every
+ regex must match a warning that was issued by `cov`. It is OK for
+ extra warnings to be issued by `cov` that are not matched by any regex.
+
+ If `warnings` is empty, then `cov` is not allowed to issue any
+ warnings.
+
+ """
saved_warnings = []
def capture_warning(msg):
"""A fake implementation of Coverage._warn, to capture warnings."""
@@ -284,12 +293,18 @@ class CoverageTest(
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))
+ if warnings:
+ 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))
+ else:
+ # No warnings expected. Raise if any warnings happened.
+ if saved_warnings:
+ self.fail("Unexpected warnings: %r" % (saved_warnings,))
+ finally:
cov._warn = original_warn
def nice_file(self, *fparts):
diff --git a/tests/test_testing.py b/tests/test_testing.py
index 9776acb4..d86207ed 100644
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -119,6 +119,19 @@ class CoverageTestTest(CoverageTest):
with self.assert_warnings(cov, ["Hello there!"]):
raise ZeroDivisionError("oops")
+ def test_assert_no_warnings(self):
+ cov = coverage.Coverage()
+
+ # Happy path: no warnings.
+ with self.assert_warnings(cov, []):
+ pass
+
+ # If you said there would be no warnings, and there were, fail!
+ warn_regex = r"Unexpected warnings: \['Watch out!'\]"
+ with self.assertRaisesRegex(AssertionError, warn_regex):
+ with self.assert_warnings(cov, []):
+ cov._warn("Watch out!")
+
def test_sub_python_is_this_python(self):
# Try it with a Python command.
self.set_environ('COV_FOOBAR', 'XYZZY')