diff options
-rw-r--r-- | tests/coveragetest.py | 2 | ||||
-rw-r--r-- | tests/test_api.py | 3 | ||||
-rw-r--r-- | tests/test_config.py | 12 | ||||
-rw-r--r-- | tests/test_execfile.py | 8 | ||||
-rw-r--r-- | tests/test_plugins.py | 7 | ||||
-rw-r--r-- | tests/test_templite.py | 2 | ||||
-rw-r--r-- | tests/test_testing.py | 38 |
7 files changed, 47 insertions, 25 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index c0a8fb5a..ddade452 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -358,7 +358,7 @@ class CoverageTest( def assert_file_count(self, pattern, count): """Assert that there are `count` files matching `pattern`.""" - files = glob.glob(pattern) + files = sorted(glob.glob(pattern)) msg = "There should be {} files matching {!r}, but there are these: {}" msg = msg.format(count, pattern, files) self.assertEqual(len(files), count, msg) diff --git a/tests/test_api.py b/tests/test_api.py index 22020cb4..e838e6b7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -263,7 +263,8 @@ class ApiTest(CoverageTest): # empty summary reports raise exception, just like the xml report cov = coverage.Coverage() cov.erase() - self.assertRaises(CoverageException, cov.report) + with self.assertRaisesRegex(CoverageException, "No data to report."): + cov.report() def make_code1_code2(self): """Create the code1.py and code2.py files.""" diff --git a/tests/test_config.py b/tests/test_config.py index 513522ee..7b019f94 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -214,13 +214,13 @@ class ConfigTest(CoverageTest): def test_tweak_error_checking(self): # Trying to set an unknown config value raises an error. cov = coverage.Coverage() - with self.assertRaises(CoverageException): + with self.assertRaisesRegex(CoverageException, "No such option: 'run:xyzzy'"): cov.set_option("run:xyzzy", 12) - with self.assertRaises(CoverageException): + with self.assertRaisesRegex(CoverageException, "No such option: 'xyzzy:foo'"): cov.set_option("xyzzy:foo", 12) - with self.assertRaises(CoverageException): + with self.assertRaisesRegex(CoverageException, "No such option: 'run:xyzzy'"): _ = cov.get_option("run:xyzzy") - with self.assertRaises(CoverageException): + with self.assertRaisesRegex(CoverageException, "No such option: 'xyzzy:foo'"): _ = cov.get_option("xyzzy:foo") def test_tweak_plugin_options(self): @@ -229,12 +229,12 @@ class ConfigTest(CoverageTest): cov.set_option("run:plugins", ["fooey.plugin", "xyzzy.coverage.plugin"]) cov.set_option("fooey.plugin:xyzzy", 17) cov.set_option("xyzzy.coverage.plugin:plugh", ["a", "b"]) - with self.assertRaises(CoverageException): + with self.assertRaisesRegex(CoverageException, "No such option: 'no_such.plugin:foo'"): cov.set_option("no_such.plugin:foo", 23) self.assertEqual(cov.get_option("fooey.plugin:xyzzy"), 17) self.assertEqual(cov.get_option("xyzzy.coverage.plugin:plugh"), ["a", "b"]) - with self.assertRaises(CoverageException): + with self.assertRaisesRegex(CoverageException, "No such option: 'no_such.plugin:foo'"): _ = cov.get_option("no_such.plugin:foo") def test_unknown_option(self): diff --git a/tests/test_execfile.py b/tests/test_execfile.py index ced4c28f..340df4cf 100644 --- a/tests/test_execfile.py +++ b/tests/test_execfile.py @@ -82,7 +82,7 @@ class RunFileTest(CoverageTest): self.assertEqual(self.stdout(), "a is 1\n") def test_no_such_file(self): - with self.assertRaises(NoSource): + with self.assertRaisesRegex(NoSource, "No file to run: 'xyzzy.py'"): run_python_file(["xyzzy.py"]) def test_directory_with_main(self): @@ -205,11 +205,11 @@ class RunModuleTest(UsingModulesMixin, CoverageTest): self.assertEqual(self.stdout(), "pkg1.__init__: pkg1\npkg1.__init__: __main__\n") def test_no_such_module(self): - with self.assertRaises(NoSource): + with self.assertRaisesRegex(NoSource, "No module named '?i_dont_exist'?"): run_python_module(["i_dont_exist"]) - with self.assertRaises(NoSource): + with self.assertRaisesRegex(NoSource, "No module named '?i'?"): run_python_module(["i.dont_exist"]) - with self.assertRaises(NoSource): + with self.assertRaisesRegex(NoSource, "No module named '?i'?"): run_python_module(["i.dont.exist"]) def test_no_main(self): diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 21653a61..f5a17619 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -121,7 +121,7 @@ class LoadPluginsTest(CoverageTest): self.assertEqual(plugins[1].options, {'a': 'second'}) def test_cant_import(self): - with self.assertRaises(ImportError): + with self.assertRaisesRegex(ImportError, "No module named '?plugin_not_there'?"): _ = Plugins.load_plugins(["plugin_not_there"], None) def test_plugin_must_define_coverage_init(self): @@ -160,7 +160,7 @@ class PluginTest(CoverageTest): def test_missing_plugin_raises_import_error(self): # Prove that a missing plugin will raise an ImportError. - with self.assertRaises(ImportError): + with self.assertRaisesRegex(ImportError, "No module named '?does_not_exist_woijwoicweo'?"): cov = coverage.Coverage() cov.set_option("run:plugins", ["does_not_exist_woijwoicweo"]) cov.start() @@ -1137,7 +1137,8 @@ class DynamicContextPluginOtherTracersTest(CoverageTest): cov = coverage.Coverage() cov.set_option("run:plugins", ['context_plugin']) - with self.assertRaises(CoverageException): + msg = "Can't support dynamic contexts with PyTracer" + with self.assertRaisesRegex(CoverageException, msg): cov.start() cov.stop() # pragma: nested diff --git a/tests/test_templite.py b/tests/test_templite.py index 3b1e38af..04150024 100644 --- a/tests/test_templite.py +++ b/tests/test_templite.py @@ -64,7 +64,7 @@ class TempliteTest(CoverageTest): def test_undefined_variables(self): # Using undefined names is an error. - with self.assertRaises(Exception): + with self.assertRaisesRegex(Exception, "'name'"): self.try_render("Hi, {{name}}!") def test_pipes(self): diff --git a/tests/test_testing.py b/tests/test_testing.py index 79d8dcae..2d9aeb21 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -6,6 +6,7 @@ import datetime import os +import re import sys import pytest @@ -52,9 +53,11 @@ class CoverageTestTest(CoverageTest): self.make_file("whoville.txt", "We are here!") self.assert_exists("whoville.txt") self.assert_doesnt_exist("shadow.txt") - with self.assertRaises(AssertionError): + msg = "False is not true : File 'whoville.txt' shouldn't exist" + with self.assertRaisesRegex(AssertionError, msg): self.assert_doesnt_exist("whoville.txt") - with self.assertRaises(AssertionError): + msg = "False is not true : File 'shadow.txt' should exist" + with self.assertRaisesRegex(AssertionError, msg): self.assert_exists("shadow.txt") def test_file_count(self): @@ -65,22 +68,39 @@ class CoverageTestTest(CoverageTest): self.assert_file_count("*c*.txt", 2) self.assert_file_count("afile.*", 1) self.assert_file_count("*.q", 0) - with self.assertRaises(AssertionError): + msg = re.escape( + "3 != 13 : There should be 13 files matching 'a*.txt', but there are these: " + "['abcde.txt', 'afile.txt', 'axczz.txt']" + ) + with self.assertRaisesRegex(AssertionError, msg): self.assert_file_count("a*.txt", 13) - with self.assertRaises(AssertionError): + msg = re.escape( + "2 != 12 : There should be 12 files matching '*c*.txt', but there are these: " + "['abcde.txt', 'axczz.txt']" + ) + with self.assertRaisesRegex(AssertionError, msg): self.assert_file_count("*c*.txt", 12) - with self.assertRaises(AssertionError): + msg = re.escape( + "1 != 11 : There should be 11 files matching 'afile.*', but there are these: " + "['afile.txt']" + ) + with self.assertRaisesRegex(AssertionError, msg): self.assert_file_count("afile.*", 11) - with self.assertRaises(AssertionError): + msg = re.escape( + "0 != 10 : There should be 10 files matching '*.q', but there are these: []" + ) + with self.assertRaisesRegex(AssertionError, msg): self.assert_file_count("*.q", 10) def test_assert_startwith(self): self.assert_starts_with("xyzzy", "xy") self.assert_starts_with("xyz\nabc", "xy") self.assert_starts_with("xyzzy", ("x", "z")) - with self.assertRaises(AssertionError): + msg = re.escape("'xyz' doesn't start with 'a'") + with self.assertRaisesRegex(AssertionError, msg): self.assert_starts_with("xyz", "a") - with self.assertRaises(AssertionError): + msg = re.escape("'xyz\\nabc' doesn't start with 'a'") + with self.assertRaisesRegex(AssertionError, msg): self.assert_starts_with("xyz\nabc", "a") def test_assert_recent_datetime(self): @@ -149,7 +169,7 @@ class CoverageTestTest(CoverageTest): cov._warn("Bye") # assert_warnings shouldn't hide a real exception. - with self.assertRaises(ZeroDivisionError): + with self.assertRaisesRegex(ZeroDivisionError, "oops"): with self.assert_warnings(cov, ["Hello there!"]): raise ZeroDivisionError("oops") |