summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-11-11 07:46:11 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-11-11 07:55:10 -0500
commite30a444a623870f1f35450186c6fc26d464b49f3 (patch)
tree6018f7be2e1f35c10087af13d4145e59688be4d5 /tests
parent1b94835aac3268a32bfa4ce0df585dbb97457a06 (diff)
downloadpython-coveragepy-git-e30a444a623870f1f35450186c6fc26d464b49f3.tar.gz
test(refactor): convert looping tests to parametrize
Diffstat (limited to 'tests')
-rw-r--r--tests/test_concurrency.py24
-rw-r--r--tests/test_config.py57
-rw-r--r--tests/test_parser.py15
3 files changed, 45 insertions, 51 deletions
diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py
index 6db75e69..0f5b07eb 100644
--- a/tests/test_concurrency.py
+++ b/tests/test_concurrency.py
@@ -429,7 +429,11 @@ class MultiprocessingTest(CoverageTest):
code, expected_out, eventlet, nprocs, concurrency="multiprocessing,eventlet"
)
- def test_multiprocessing_with_branching(self):
+ @pytest.mark.parametrize("start_method", ["fork", "spawn"])
+ def test_multiprocessing_with_branching(self, start_method):
+ if start_method not in multiprocessing.get_all_start_methods():
+ pytest.skip(f"{start_method} not supported here")
+
nprocs = 3
upto = 30
code = (SQUARE_OR_CUBE_WORK + MULTI_CODE).format(NPROCS=nprocs, UPTO=upto)
@@ -443,19 +447,15 @@ class MultiprocessingTest(CoverageTest):
omit = */site-packages/*
""")
- for start_method in ["fork", "spawn"]:
- if start_method and start_method not in multiprocessing.get_all_start_methods():
- continue
-
- out = self.run_command(f"coverage run --rcfile=multi.rc multi.py {start_method}")
- assert out.rstrip() == expected_out
+ out = self.run_command(f"coverage run --rcfile=multi.rc multi.py {start_method}")
+ assert out.rstrip() == expected_out
- out = self.run_command("coverage combine -q") # sneak in a test of -q
- assert out == ""
- out = self.run_command("coverage report -m")
+ out = self.run_command("coverage combine -q") # sneak in a test of -q
+ assert out == ""
+ out = self.run_command("coverage report -m")
- last_line = self.squeezed_lines(out)[-1]
- assert re.search(r"TOTAL \d+ 0 \d+ 0 100%", last_line)
+ last_line = self.squeezed_lines(out)[-1]
+ assert re.search(r"TOTAL \d+ 0 \d+ 0 100%", last_line)
def test_multiprocessing_bootstrap_error_handling(self):
# An exception during bootstrapping will be reported.
diff --git a/tests/test_config.py b/tests/test_config.py
index ad3b446a..411a456f 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -162,31 +162,28 @@ class ConfigTest(CoverageTest):
with pytest.raises(CoverageException, match=msg):
coverage.Coverage()
- def test_parse_errors(self):
+ @pytest.mark.parametrize("bad_config, msg", [
+ ("[run]\ntimid = maybe?\n", r"maybe[?]"),
+ ("timid = 1\n", r"no section headers"),
+ ("[run\n", r"\[run"),
+ ("[report]\nexclude_lines = foo(\n",
+ r"Invalid \[report\].exclude_lines value 'foo\(': " +
+ r"(unbalanced parenthesis|missing \))"),
+ ("[report]\npartial_branches = foo[\n",
+ r"Invalid \[report\].partial_branches value 'foo\[': " +
+ r"(unexpected end of regular expression|unterminated character set)"),
+ ("[report]\npartial_branches_always = foo***\n",
+ r"Invalid \[report\].partial_branches_always value " +
+ r"'foo\*\*\*': " +
+ r"multiple repeat"),
+ ])
+ def test_parse_errors(self, bad_config, msg):
# Im-parsable values raise CoverageException, with details.
- bad_configs_and_msgs = [
- ("[run]\ntimid = maybe?\n", r"maybe[?]"),
- ("timid = 1\n", r"no section headers"),
- ("[run\n", r"\[run"),
- ("[report]\nexclude_lines = foo(\n",
- r"Invalid \[report\].exclude_lines value 'foo\(': " +
- r"(unbalanced parenthesis|missing \))"),
- ("[report]\npartial_branches = foo[\n",
- r"Invalid \[report\].partial_branches value 'foo\[': " +
- r"(unexpected end of regular expression|unterminated character set)"),
- ("[report]\npartial_branches_always = foo***\n",
- r"Invalid \[report\].partial_branches_always value " +
- r"'foo\*\*\*': " +
- r"multiple repeat"),
- ]
-
- for bad_config, msg in bad_configs_and_msgs:
- print(f"Trying {bad_config!r}")
- self.make_file(".coveragerc", bad_config)
- with pytest.raises(CoverageException, match=msg):
- coverage.Coverage()
+ self.make_file(".coveragerc", bad_config)
+ with pytest.raises(CoverageException, match=msg):
+ coverage.Coverage()
- @pytest.mark.parametrize("bad_config,msg", [
+ @pytest.mark.parametrize("bad_config, msg", [
("[tool.coverage.run]\ntimid = \"maybe?\"\n", r"maybe[?]"),
("[tool.coverage.run\n", None),
('[tool.coverage.report]\nexclude_lines = ["foo("]\n',
@@ -681,17 +678,13 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest):
assert cov.config.exclude_list == ["first", "✘weirdo", "third"]
assert cov.config.html_title == "tabblo & «ταБЬℓσ» # numbers"
- def test_unreadable_config(self):
+ @pytest.mark.parametrize("bad_file", ["nosuchfile.txt", "."])
+ def test_unreadable_config(self, bad_file):
# If a config file is explicitly specified, then it is an error for it
# to not be readable.
- bad_files = [
- "nosuchfile.txt",
- ".",
- ]
- for bad_file in bad_files:
- msg = f"Couldn't read {bad_file!r} as a config file"
- with pytest.raises(CoverageException, match=msg):
- coverage.Coverage(config_file=bad_file)
+ msg = f"Couldn't read {bad_file!r} as a config file"
+ 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")
diff --git a/tests/test_parser.py b/tests/test_parser.py
index 6dcfc00a..1e509035 100644
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -444,7 +444,10 @@ class ParserFileTest(CoverageTest):
parser.parse_source()
return parser
- def test_line_endings(self):
+ @pytest.mark.parametrize("slug, newline", [
+ ("unix", "\n"), ("dos", "\r\n"), ("mac", "\r"),
+ ])
+ def test_line_endings(self, slug, newline):
text = """\
# check some basic branch counting
class Foo:
@@ -458,12 +461,10 @@ class ParserFileTest(CoverageTest):
pass
"""
counts = { 2:2, 3:1, 4:2, 5:1, 7:1, 9:2, 10:1 }
- name_endings = (("unix", "\n"), ("dos", "\r\n"), ("mac", "\r"))
- for fname, newline in name_endings:
- fname = fname + ".py"
- self.make_file(fname, text, newline=newline)
- parser = self.parse_file(fname)
- assert parser.exit_counts() == counts, f"Wrong for {fname!r}"
+ fname = slug + ".py"
+ self.make_file(fname, text, newline=newline)
+ parser = self.parse_file(fname)
+ assert parser.exit_counts() == counts, f"Wrong for {fname!r}"
def test_encoding(self):
self.make_file("encoded.py", """\