Skip to content

Commit

Permalink
test(refactor): convert looping tests to parametrize
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 11, 2021
1 parent 1b94835 commit e30a444
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
24 changes: 12 additions & 12 deletions tests/test_concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ def test_multiprocessing_and_gevent(self):
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)
Expand All @@ -443,19 +447,15 @@ def test_multiprocessing_with_branching(self):
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.
Expand Down
57 changes: 25 additions & 32 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,31 +162,28 @@ def test_missing_rcfile_from_environment(self):
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',
Expand Down Expand Up @@ -681,17 +678,13 @@ def test_non_ascii(self):
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")
Expand Down
15 changes: 8 additions & 7 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ def parse_file(self, filename):
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:
Expand All @@ -458,12 +461,10 @@ class Bar:
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", """\
Expand Down

0 comments on commit e30a444

Please sign in to comment.