diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-01-31 07:16:56 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-01-31 07:16:56 -0500 |
commit | 843de4ea235e7eee3ff24a39a2f8b14da9ef0db0 (patch) | |
tree | 8a4d8435595334318b5e38ef42da803e512acd4f /tests/test_config.py | |
parent | 4fc64a97ce779c2d6bb972f0003b9b9f00e62c3a (diff) | |
download | python-coveragepy-git-843de4ea235e7eee3ff24a39a2f8b14da9ef0db0.tar.gz |
refactor: unittest2pytest -w tests
One step of moving to pure pytest tests.
Diffstat (limited to 'tests/test_config.py')
-rw-r--r-- | tests/test_config.py | 261 |
1 files changed, 128 insertions, 133 deletions
diff --git a/tests/test_config.py b/tests/test_config.py index 4225540c..0a742f3d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -13,6 +13,7 @@ from coverage.misc import CoverageException from tests.coveragetest import CoverageTest, UsingModulesMixin from tests.helpers import without_module +import pytest class ConfigTest(CoverageTest): @@ -21,17 +22,17 @@ class ConfigTest(CoverageTest): def test_default_config(self): # Just constructing a coverage() object gets the right defaults. cov = coverage.Coverage() - self.assertFalse(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, ".coverage") + assert not cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == ".coverage" def test_arguments(self): # Arguments to the constructor are applied to the configuration. cov = coverage.Coverage(timid=True, data_file="fooey.dat", concurrency="multiprocessing") - self.assertTrue(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, "fooey.dat") - self.assertEqual(cov.config.concurrency, ["multiprocessing"]) + assert cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == "fooey.dat" + assert cov.config.concurrency == ["multiprocessing"] def test_config_file(self): # A .coveragerc file will be read into the configuration. @@ -42,9 +43,9 @@ class ConfigTest(CoverageTest): data_file = .hello_kitty.data """) cov = coverage.Coverage() - self.assertTrue(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, ".hello_kitty.data") + assert cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == ".hello_kitty.data" def test_named_config_file(self): # You can name the config file what you like. @@ -55,9 +56,9 @@ class ConfigTest(CoverageTest): data_file = delete.me """) cov = coverage.Coverage(config_file="my_cov.ini") - self.assertTrue(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, "delete.me") + assert cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == "delete.me" def test_toml_config_file(self): # A .coveragerc file will be read into the configuration. @@ -79,18 +80,16 @@ class ConfigTest(CoverageTest): hello = "world" """) cov = coverage.Coverage(config_file="pyproject.toml") - self.assertTrue(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.concurrency, [u"a", u"b"]) - self.assertEqual(cov.config.data_file, u".hello_kitty.data") - self.assertEqual(cov.config.plugins, [u"plugins.a_plugin"]) - self.assertEqual(cov.config.precision, 3) - self.assertEqual(cov.config.html_title, u"tabblo & «ταБЬℓσ»") - self.assertAlmostEqual(cov.config.fail_under, 90.5) - self.assertEqual( - cov.config.get_plugin_options("plugins.a_plugin"), + assert cov.config.timid + assert not cov.config.branch + assert cov.config.concurrency == [u"a", u"b"] + assert cov.config.data_file == u".hello_kitty.data" + assert cov.config.plugins == [u"plugins.a_plugin"] + assert cov.config.precision == 3 + assert cov.config.html_title == u"tabblo & «ταБЬℓσ»" + assert round(abs(cov.config.fail_under-90.5), 7) == 0 + assert cov.config.get_plugin_options("plugins.a_plugin") == \ {u"hello": u"world"} - ) # Test that our class doesn't reject integers when loading floats self.make_file("pyproject.toml", """\ @@ -99,8 +98,8 @@ class ConfigTest(CoverageTest): fail_under = 90 """) cov = coverage.Coverage(config_file="pyproject.toml") - self.assertAlmostEqual(cov.config.fail_under, 90) - self.assertIsInstance(cov.config.fail_under, float) + assert round(abs(cov.config.fail_under-90), 7) == 0 + assert isinstance(cov.config.fail_under, float) def test_ignored_config_file(self): # You can disable reading the .coveragerc file. @@ -110,9 +109,9 @@ class ConfigTest(CoverageTest): data_file = delete.me """) cov = coverage.Coverage(config_file=False) - self.assertFalse(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, ".coverage") + assert not cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == ".coverage" def test_config_file_then_args(self): # The arguments override the .coveragerc file. @@ -122,9 +121,9 @@ class ConfigTest(CoverageTest): data_file = weirdo.file """) cov = coverage.Coverage(timid=False, data_file=".mycov") - self.assertFalse(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, ".mycov") + assert not cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == ".mycov" def test_data_file_from_environment(self): # There's an environment variable for the data_file. @@ -135,10 +134,10 @@ class ConfigTest(CoverageTest): """) self.set_environ("COVERAGE_FILE", "fromenv.dat") cov = coverage.Coverage() - self.assertEqual(cov.config.data_file, "fromenv.dat") + assert cov.config.data_file == "fromenv.dat" # But the constructor arguments override the environment variable. cov = coverage.Coverage(data_file="fromarg.dat") - self.assertEqual(cov.config.data_file, "fromarg.dat") + assert cov.config.data_file == "fromarg.dat" def test_debug_from_environment(self): self.make_file(".coveragerc", """\ @@ -147,7 +146,7 @@ class ConfigTest(CoverageTest): """) self.set_environ("COVERAGE_DEBUG", "callers, fooey") cov = coverage.Coverage() - self.assertEqual(cov.config.debug, ["dataio", "pids", "callers", "fooey"]) + assert cov.config.debug == ["dataio", "pids", "callers", "fooey"] def test_rcfile_from_environment(self): self.make_file("here.ini", """\ @@ -156,12 +155,12 @@ class ConfigTest(CoverageTest): """) self.set_environ("COVERAGE_RCFILE", "here.ini") cov = coverage.Coverage() - self.assertEqual(cov.config.data_file, "overthere.dat") + assert cov.config.data_file == "overthere.dat" def test_missing_rcfile_from_environment(self): self.set_environ("COVERAGE_RCFILE", "nowhere.ini") msg = "Couldn't read 'nowhere.ini' as a config file" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage() def test_parse_errors(self): @@ -185,7 +184,7 @@ class ConfigTest(CoverageTest): for bad_config, msg in bad_configs_and_msgs: print("Trying %r" % bad_config) self.make_file(".coveragerc", bad_config) - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage() def test_toml_parse_errors(self): @@ -211,7 +210,7 @@ class ConfigTest(CoverageTest): for bad_config, msg in bad_configs_and_msgs: print("Trying %r" % bad_config) self.make_file("pyproject.toml", bad_config) - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage() def test_environment_vars_in_config(self): @@ -232,12 +231,10 @@ class ConfigTest(CoverageTest): self.set_environ("THING", "ZZZ") self.set_environ("OKAY", "yes") cov = coverage.Coverage() - self.assertEqual(cov.config.data_file, "hello-world.fooey") - self.assertEqual(cov.config.branch, True) - self.assertEqual( - cov.config.exclude_list, + assert cov.config.data_file == "hello-world.fooey" + assert cov.config.branch == True + assert cov.config.exclude_list == \ ["the_$one", "anotherZZZ", "xZZZy", "xy", "huh${X}what"] - ) def test_environment_vars_in_toml_config(self): # Config files can have $envvars in them. @@ -258,12 +255,10 @@ class ConfigTest(CoverageTest): self.set_environ("DATA_FILE", "hello-world") self.set_environ("THING", "ZZZ") cov = coverage.Coverage() - self.assertEqual(cov.config.data_file, "hello-world.fooey") - self.assertEqual(cov.config.branch, True) - self.assertEqual( - cov.config.exclude_list, + assert cov.config.data_file == "hello-world.fooey" + assert cov.config.branch == True + assert cov.config.exclude_list == \ ["the_$one", "anotherZZZ", "xZZZy", "xy", "huh${X}what"] - ) def test_tilde_in_config(self): # Config entries that are file paths can be tilde-expanded. @@ -296,11 +291,11 @@ class ConfigTest(CoverageTest): with mock.patch.object(coverage.config.os.path, 'expanduser', new=expanduser): cov = coverage.Coverage() - self.assertEqual(cov.config.data_file, "/Users/me/data.file") - self.assertEqual(cov.config.html_dir, "/Users/joe/html_dir") - self.assertEqual(cov.config.xml_output, "/Users/me/somewhere/xml.out") - self.assertEqual(cov.config.exclude_list, ["~/data.file", "~joe/html_dir"]) - self.assertEqual(cov.config.paths, {'mapping': ['/Users/me/src', '/Users/joe/source']}) + assert cov.config.data_file == "/Users/me/data.file" + assert cov.config.html_dir == "/Users/joe/html_dir" + assert cov.config.xml_output == "/Users/me/somewhere/xml.out" + assert cov.config.exclude_list == ["~/data.file", "~joe/html_dir"] + assert cov.config.paths == {'mapping': ['/Users/me/src', '/Users/joe/source']} def test_tilde_in_toml_config(self): # Config entries that are file paths can be tilde-expanded. @@ -329,23 +324,23 @@ class ConfigTest(CoverageTest): with mock.patch.object(coverage.config.os.path, 'expanduser', new=expanduser): cov = coverage.Coverage() - self.assertEqual(cov.config.data_file, "/Users/me/data.file") - self.assertEqual(cov.config.html_dir, "/Users/joe/html_dir") - self.assertEqual(cov.config.xml_output, "/Users/me/somewhere/xml.out") - self.assertEqual(cov.config.exclude_list, ["~/data.file", "~joe/html_dir"]) + assert cov.config.data_file == "/Users/me/data.file" + assert cov.config.html_dir == "/Users/joe/html_dir" + assert cov.config.xml_output == "/Users/me/somewhere/xml.out" + assert cov.config.exclude_list == ["~/data.file", "~joe/html_dir"] def test_tweaks_after_constructor(self): # set_option can be used after construction to affect the config. cov = coverage.Coverage(timid=True, data_file="fooey.dat") cov.set_option("run:timid", False) - self.assertFalse(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, "fooey.dat") + assert not cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == "fooey.dat" - self.assertFalse(cov.get_option("run:timid")) - self.assertFalse(cov.get_option("run:branch")) - self.assertEqual(cov.get_option("run:data_file"), "fooey.dat") + assert not cov.get_option("run:timid") + assert not cov.get_option("run:branch") + assert cov.get_option("run:data_file") == "fooey.dat" def test_tweaks_paths_after_constructor(self): self.make_file(".coveragerc", """\ @@ -363,24 +358,24 @@ class ConfigTest(CoverageTest): old_paths["second"] = ["/second/a", "/second/b"] cov = coverage.Coverage() paths = cov.get_option("paths") - self.assertEqual(paths, old_paths) + assert paths == old_paths new_paths = OrderedDict() new_paths['magic'] = ['src', 'ok'] cov.set_option("paths", new_paths) - self.assertEqual(cov.get_option("paths"), new_paths) + assert cov.get_option("paths") == new_paths def test_tweak_error_checking(self): # Trying to set an unknown config value raises an error. cov = coverage.Coverage() - with self.assertRaisesRegex(CoverageException, "No such option: 'run:xyzzy'"): + with pytest.raises(CoverageException, match="No such option: 'run:xyzzy'"): cov.set_option("run:xyzzy", 12) - with self.assertRaisesRegex(CoverageException, "No such option: 'xyzzy:foo'"): + with pytest.raises(CoverageException, match="No such option: 'xyzzy:foo'"): cov.set_option("xyzzy:foo", 12) - with self.assertRaisesRegex(CoverageException, "No such option: 'run:xyzzy'"): + with pytest.raises(CoverageException, match="No such option: 'run:xyzzy'"): _ = cov.get_option("run:xyzzy") - with self.assertRaisesRegex(CoverageException, "No such option: 'xyzzy:foo'"): + with pytest.raises(CoverageException, match="No such option: 'xyzzy:foo'"): _ = cov.get_option("xyzzy:foo") def test_tweak_plugin_options(self): @@ -389,12 +384,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.assertRaisesRegex(CoverageException, "No such option: 'no_such.plugin:foo'"): + with pytest.raises(CoverageException, match="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.assertRaisesRegex(CoverageException, "No such option: 'no_such.plugin:foo'"): + assert cov.get_option("fooey.plugin:xyzzy") == 17 + assert cov.get_option("xyzzy.coverage.plugin:plugh") == ["a", "b"] + with pytest.raises(CoverageException, match="No such option: 'no_such.plugin:foo'"): _ = cov.get_option("no_such.plugin:foo") def test_unknown_option(self): @@ -403,7 +398,7 @@ class ConfigTest(CoverageTest): xyzzy = 17 """) msg = r"Unrecognized option '\[run\] xyzzy=' in config file .coveragerc" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): _ = coverage.Coverage() def test_unknown_option_toml(self): @@ -412,7 +407,7 @@ class ConfigTest(CoverageTest): xyzzy = 17 """) msg = r"Unrecognized option '\[tool.coverage.run\] xyzzy=' in config file pyproject.toml" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): _ = coverage.Coverage() def test_misplaced_option(self): @@ -421,7 +416,7 @@ class ConfigTest(CoverageTest): branch = True """) msg = r"Unrecognized option '\[report\] branch=' in config file .coveragerc" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): _ = coverage.Coverage() def test_unknown_option_in_other_ini_file(self): @@ -430,7 +425,7 @@ class ConfigTest(CoverageTest): huh = what? """) msg = (r"Unrecognized option '\[coverage:run\] huh=' in config file setup.cfg") - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): _ = coverage.Coverage() def test_note_is_obsolete(self): @@ -546,49 +541,49 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): def assert_config_settings_are_correct(self, cov): """Check that `cov` has all the settings from LOTSA_SETTINGS.""" - self.assertTrue(cov.config.timid) - self.assertEqual(cov.config.data_file, "something_or_other.dat") - self.assertTrue(cov.config.branch) - self.assertTrue(cov.config.cover_pylib) - self.assertEqual(cov.config.debug, ["callers", "pids", "dataio"]) - self.assertTrue(cov.config.parallel) - self.assertEqual(cov.config.concurrency, ["thread"]) - self.assertEqual(cov.config.source, ["myapp"]) - self.assertEqual(cov.config.source_pkgs, ["ned"]) - self.assertEqual(cov.config.disable_warnings, ["abcd", "efgh"]) - - self.assertEqual(cov.get_exclude_list(), ["if 0:", r"pragma:?\s+no cover", "another_tab"]) - self.assertTrue(cov.config.ignore_errors) - self.assertEqual(cov.config.run_omit, ["twenty"]) - self.assertEqual(cov.config.report_omit, ["one", "another", "some_more", "yet_more"]) - self.assertEqual(cov.config.report_include, ["thirty"]) - self.assertEqual(cov.config.precision, 3) - - self.assertEqual(cov.config.partial_list, [r"pragma:?\s+no branch"]) - self.assertEqual(cov.config.partial_always_list, ["if 0:", "while True:"]) - self.assertEqual(cov.config.plugins, ["plugins.a_plugin", "plugins.another"]) - self.assertTrue(cov.config.show_missing) - self.assertTrue(cov.config.skip_covered) - self.assertTrue(cov.config.skip_empty) - self.assertEqual(cov.config.html_dir, r"c:\tricky\dir.somewhere") - self.assertEqual(cov.config.extra_css, "something/extra.css") - self.assertEqual(cov.config.html_title, "Title & nums # nums!") - - self.assertEqual(cov.config.xml_output, "mycov.xml") - self.assertEqual(cov.config.xml_package_depth, 17) - - self.assertEqual(cov.config.paths, { + assert cov.config.timid + assert cov.config.data_file == "something_or_other.dat" + assert cov.config.branch + assert cov.config.cover_pylib + assert cov.config.debug == ["callers", "pids", "dataio"] + assert cov.config.parallel + assert cov.config.concurrency == ["thread"] + assert cov.config.source == ["myapp"] + assert cov.config.source_pkgs == ["ned"] + assert cov.config.disable_warnings == ["abcd", "efgh"] + + assert cov.get_exclude_list() == ["if 0:", r"pragma:?\s+no cover", "another_tab"] + assert cov.config.ignore_errors + assert cov.config.run_omit == ["twenty"] + assert cov.config.report_omit == ["one", "another", "some_more", "yet_more"] + assert cov.config.report_include == ["thirty"] + assert cov.config.precision == 3 + + assert cov.config.partial_list == [r"pragma:?\s+no branch"] + assert cov.config.partial_always_list == ["if 0:", "while True:"] + assert cov.config.plugins == ["plugins.a_plugin", "plugins.another"] + assert cov.config.show_missing + assert cov.config.skip_covered + assert cov.config.skip_empty + assert cov.config.html_dir == r"c:\tricky\dir.somewhere" + assert cov.config.extra_css == "something/extra.css" + assert cov.config.html_title == "Title & nums # nums!" + + assert cov.config.xml_output == "mycov.xml" + assert cov.config.xml_package_depth == 17 + + assert cov.config.paths == { 'source': ['.', '/home/ned/src/'], 'other': ['other', '/home/ned/other', 'c:\\Ned\\etc'] - }) + } - self.assertEqual(cov.config.get_plugin_options("plugins.a_plugin"), { + assert cov.config.get_plugin_options("plugins.a_plugin") == { 'hello': 'world', 'names': 'Jane/John/Jenny', - }) - self.assertEqual(cov.config.get_plugin_options("plugins.another"), {}) - self.assertEqual(cov.config.json_show_contexts, True) - self.assertEqual(cov.config.json_pretty_print, True) + } + assert cov.config.get_plugin_options("plugins.another") == {} + assert cov.config.json_show_contexts == True + assert cov.config.json_pretty_print == True def test_config_file_settings(self): self.make_file(".coveragerc", self.LOTSA_SETTINGS.format(section="")) @@ -633,9 +628,9 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): branch = true """) cov = coverage.Coverage() - self.assertEqual(cov.config.run_include, ["foo"]) - self.assertEqual(cov.config.run_omit, None) - self.assertEqual(cov.config.branch, False) + assert cov.config.run_include == ["foo"] + assert cov.config.run_omit == None + assert cov.config.branch == False def test_setupcfg_only_if_not_coveragerc(self): self.check_other_not_read_if_coveragerc("setup.cfg") @@ -651,8 +646,8 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): branch = true """) cov = coverage.Coverage() - self.assertEqual(cov.config.run_omit, None) - self.assertEqual(cov.config.branch, False) + assert cov.config.run_omit == None + assert cov.config.branch == False def test_setupcfg_only_if_prefixed(self): self.check_other_config_need_prefixes("setup.cfg") @@ -689,8 +684,8 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): self.set_environ("TOX_ENVNAME", "weirdo") cov = coverage.Coverage() - self.assertEqual(cov.config.exclude_list, ["first", "✘weirdo", "third"]) - self.assertEqual(cov.config.html_title, "tabblo & «ταБЬℓσ» # numbers") + assert cov.config.exclude_list == ["first", "✘weirdo", "third"] + assert cov.config.html_title == "tabblo & «ταБЬℓσ» # numbers" def test_unreadable_config(self): # If a config file is explicitly specified, then it is an error for it @@ -701,20 +696,20 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): ] for bad_file in bad_files: msg = "Couldn't read %r as a config file" % bad_file - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage(config_file=bad_file) def test_nocoveragerc_file_when_specified(self): cov = coverage.Coverage(config_file=".coveragerc") - self.assertFalse(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, ".coverage") + assert not cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == ".coverage" def test_no_toml_installed_no_toml(self): # Can't read a toml file that doesn't exist. with without_module(coverage.tomlconfig, 'toml'): msg = "Couldn't read 'cov.toml' as a config file" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage(config_file="cov.toml") def test_no_toml_installed_explicit_toml(self): @@ -722,7 +717,7 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): self.make_file("cov.toml", "# A toml file!") with without_module(coverage.tomlconfig, 'toml'): msg = "Can't read 'cov.toml' without TOML support" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage(config_file="cov.toml") def test_no_toml_installed_pyproject_toml(self): @@ -734,7 +729,7 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): """) with without_module(coverage.tomlconfig, 'toml'): msg = "Can't read 'pyproject.toml' without TOML support" - with self.assertRaisesRegex(CoverageException, msg): + with pytest.raises(CoverageException, match=msg): coverage.Coverage() def test_no_toml_installed_pyproject_no_coverage(self): @@ -747,6 +742,6 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): with without_module(coverage.tomlconfig, 'toml'): cov = coverage.Coverage() # We get default settings: - self.assertFalse(cov.config.timid) - self.assertFalse(cov.config.branch) - self.assertEqual(cov.config.data_file, ".coverage") + assert not cov.config.timid + assert not cov.config.branch + assert cov.config.data_file == ".coverage" |