Skip to content

Commit e30a444

Browse files
committed
test(refactor): convert looping tests to parametrize
1 parent 1b94835 commit e30a444

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

tests/test_concurrency.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,11 @@ def test_multiprocessing_and_gevent(self):
429429
code, expected_out, eventlet, nprocs, concurrency="multiprocessing,eventlet"
430430
)
431431

432-
def test_multiprocessing_with_branching(self):
432+
@pytest.mark.parametrize("start_method", ["fork", "spawn"])
433+
def test_multiprocessing_with_branching(self, start_method):
434+
if start_method not in multiprocessing.get_all_start_methods():
435+
pytest.skip(f"{start_method} not supported here")
436+
433437
nprocs = 3
434438
upto = 30
435439
code = (SQUARE_OR_CUBE_WORK + MULTI_CODE).format(NPROCS=nprocs, UPTO=upto)
@@ -443,19 +447,15 @@ def test_multiprocessing_with_branching(self):
443447
omit = */site-packages/*
444448
""")
445449

446-
for start_method in ["fork", "spawn"]:
447-
if start_method and start_method not in multiprocessing.get_all_start_methods():
448-
continue
449-
450-
out = self.run_command(f"coverage run --rcfile=multi.rc multi.py {start_method}")
451-
assert out.rstrip() == expected_out
450+
out = self.run_command(f"coverage run --rcfile=multi.rc multi.py {start_method}")
451+
assert out.rstrip() == expected_out
452452

453-
out = self.run_command("coverage combine -q") # sneak in a test of -q
454-
assert out == ""
455-
out = self.run_command("coverage report -m")
453+
out = self.run_command("coverage combine -q") # sneak in a test of -q
454+
assert out == ""
455+
out = self.run_command("coverage report -m")
456456

457-
last_line = self.squeezed_lines(out)[-1]
458-
assert re.search(r"TOTAL \d+ 0 \d+ 0 100%", last_line)
457+
last_line = self.squeezed_lines(out)[-1]
458+
assert re.search(r"TOTAL \d+ 0 \d+ 0 100%", last_line)
459459

460460
def test_multiprocessing_bootstrap_error_handling(self):
461461
# An exception during bootstrapping will be reported.

tests/test_config.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,31 +162,28 @@ def test_missing_rcfile_from_environment(self):
162162
with pytest.raises(CoverageException, match=msg):
163163
coverage.Coverage()
164164

165-
def test_parse_errors(self):
165+
@pytest.mark.parametrize("bad_config, msg", [
166+
("[run]\ntimid = maybe?\n", r"maybe[?]"),
167+
("timid = 1\n", r"no section headers"),
168+
("[run\n", r"\[run"),
169+
("[report]\nexclude_lines = foo(\n",
170+
r"Invalid \[report\].exclude_lines value 'foo\(': " +
171+
r"(unbalanced parenthesis|missing \))"),
172+
("[report]\npartial_branches = foo[\n",
173+
r"Invalid \[report\].partial_branches value 'foo\[': " +
174+
r"(unexpected end of regular expression|unterminated character set)"),
175+
("[report]\npartial_branches_always = foo***\n",
176+
r"Invalid \[report\].partial_branches_always value " +
177+
r"'foo\*\*\*': " +
178+
r"multiple repeat"),
179+
])
180+
def test_parse_errors(self, bad_config, msg):
166181
# Im-parsable values raise CoverageException, with details.
167-
bad_configs_and_msgs = [
168-
("[run]\ntimid = maybe?\n", r"maybe[?]"),
169-
("timid = 1\n", r"no section headers"),
170-
("[run\n", r"\[run"),
171-
("[report]\nexclude_lines = foo(\n",
172-
r"Invalid \[report\].exclude_lines value 'foo\(': " +
173-
r"(unbalanced parenthesis|missing \))"),
174-
("[report]\npartial_branches = foo[\n",
175-
r"Invalid \[report\].partial_branches value 'foo\[': " +
176-
r"(unexpected end of regular expression|unterminated character set)"),
177-
("[report]\npartial_branches_always = foo***\n",
178-
r"Invalid \[report\].partial_branches_always value " +
179-
r"'foo\*\*\*': " +
180-
r"multiple repeat"),
181-
]
182-
183-
for bad_config, msg in bad_configs_and_msgs:
184-
print(f"Trying {bad_config!r}")
185-
self.make_file(".coveragerc", bad_config)
186-
with pytest.raises(CoverageException, match=msg):
187-
coverage.Coverage()
182+
self.make_file(".coveragerc", bad_config)
183+
with pytest.raises(CoverageException, match=msg):
184+
coverage.Coverage()
188185

189-
@pytest.mark.parametrize("bad_config,msg", [
186+
@pytest.mark.parametrize("bad_config, msg", [
190187
("[tool.coverage.run]\ntimid = \"maybe?\"\n", r"maybe[?]"),
191188
("[tool.coverage.run\n", None),
192189
('[tool.coverage.report]\nexclude_lines = ["foo("]\n',
@@ -681,17 +678,13 @@ def test_non_ascii(self):
681678
assert cov.config.exclude_list == ["first", "✘weirdo", "third"]
682679
assert cov.config.html_title == "tabblo & «ταБЬℓσ» # numbers"
683680

684-
def test_unreadable_config(self):
681+
@pytest.mark.parametrize("bad_file", ["nosuchfile.txt", "."])
682+
def test_unreadable_config(self, bad_file):
685683
# If a config file is explicitly specified, then it is an error for it
686684
# to not be readable.
687-
bad_files = [
688-
"nosuchfile.txt",
689-
".",
690-
]
691-
for bad_file in bad_files:
692-
msg = f"Couldn't read {bad_file!r} as a config file"
693-
with pytest.raises(CoverageException, match=msg):
694-
coverage.Coverage(config_file=bad_file)
685+
msg = f"Couldn't read {bad_file!r} as a config file"
686+
with pytest.raises(CoverageException, match=msg):
687+
coverage.Coverage(config_file=bad_file)
695688

696689
def test_nocoveragerc_file_when_specified(self):
697690
cov = coverage.Coverage(config_file=".coveragerc")

tests/test_parser.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ def parse_file(self, filename):
444444
parser.parse_source()
445445
return parser
446446

447-
def test_line_endings(self):
447+
@pytest.mark.parametrize("slug, newline", [
448+
("unix", "\n"), ("dos", "\r\n"), ("mac", "\r"),
449+
])
450+
def test_line_endings(self, slug, newline):
448451
text = """\
449452
# check some basic branch counting
450453
class Foo:
@@ -458,12 +461,10 @@ class Bar:
458461
pass
459462
"""
460463
counts = { 2:2, 3:1, 4:2, 5:1, 7:1, 9:2, 10:1 }
461-
name_endings = (("unix", "\n"), ("dos", "\r\n"), ("mac", "\r"))
462-
for fname, newline in name_endings:
463-
fname = fname + ".py"
464-
self.make_file(fname, text, newline=newline)
465-
parser = self.parse_file(fname)
466-
assert parser.exit_counts() == counts, f"Wrong for {fname!r}"
464+
fname = slug + ".py"
465+
self.make_file(fname, text, newline=newline)
466+
parser = self.parse_file(fname)
467+
assert parser.exit_counts() == counts, f"Wrong for {fname!r}"
467468

468469
def test_encoding(self):
469470
self.make_file("encoded.py", """\

0 commit comments

Comments
 (0)