Skip to content

Commit 7f2cfad

Browse files
graingertionelmc
authored andcommitted
try parsing cov-fail-under as int then float
1 parent 273a695 commit 7f2cfad

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

src/pytest_cov/plugin.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def validate_report(arg):
4141
return values
4242

4343

44+
def validate_fail_under(num_str):
45+
try:
46+
return int(num_str)
47+
except ValueError:
48+
return float(num_str)
49+
50+
4451
class StoreReport(argparse.Action):
4552
def __call__(self, parser, namespace, values, option_string=None):
4653
report_type, file = values
@@ -73,7 +80,8 @@ def pytest_addoption(parser):
7380
group.addoption('--no-cov', action='store_true', default=False,
7481
help='Disable coverage report completely (useful for debuggers). '
7582
'Default: False')
76-
group.addoption('--cov-fail-under', action='store', metavar='MIN', type=float,
83+
group.addoption('--cov-fail-under', action='store', metavar='MIN',
84+
type=validate_fail_under,
7785
help='Fail if the total coverage is less than MIN.')
7886
group.addoption('--cov-append', action='store_true', default=False,
7987
help='Do not delete coverage but append to current. '
@@ -265,25 +273,19 @@ def pytest_terminal_summary(self, terminalreporter):
265273

266274
terminalreporter.write('\n' + self.cov_report.getvalue() + '\n')
267275

268-
fail_under = self.options.cov_fail_under
269-
if fail_under is not None and fail_under > 0:
270-
str_fail_under = str(
271-
round(fail_under, 2) if fail_under % 1 else int(fail_under)
272-
)
273-
if self.cov_total < fail_under:
274-
markup = {'red': True, 'bold': True}
275-
message = (
276-
'FAIL Required test coverage of %s%% not '
277-
'reached. Total coverage: %.2f%%\n'
278-
% (str_fail_under, self.cov_total)
279-
)
280-
else:
281-
markup = {'green': True}
282-
message = (
283-
'Required test coverage of %s%% '
284-
'reached. Total coverage: %.2f%%\n'
285-
% (str_fail_under, self.cov_total)
276+
if self.options.cov_fail_under is not None and self.options.cov_fail_under > 0:
277+
failed = self.cov_total < self.options.cov_fail_under
278+
markup = {'red': True, 'bold': True} if failed else {'green': True}
279+
message = (
280+
'{fail}Required test coverage of {required}% {reached}. '
281+
'Total coverage: {actual:.2f}%\n'
282+
.format(
283+
required=self.options.cov_fail_under,
284+
actual=self.cov_total,
285+
fail="FAIL " if failed else "",
286+
reached="not reached" if failed else "reached"
286287
)
288+
)
287289
terminalreporter.write(message, **markup)
288290

289291
def pytest_runtest_setup(self, item):

0 commit comments

Comments
 (0)