Skip to content

Commit 8c54284

Browse files
committed
try parsing cov-fail-under as int then float
1 parent c6c53c0 commit 8c54284

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

src/pytest_cov/plugin.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def __call__(self, parser, namespace, values, option_string=None):
5050
def pytest_addoption(parser):
5151
"""Add options to control coverage."""
5252

53+
def fail_under(num_str):
54+
try:
55+
return int(num_str)
56+
except ValueError:
57+
return float(num_str)
58+
5359
group = parser.getgroup(
5460
'cov', 'coverage reporting with distributed testing support')
5561
group.addoption('--cov', action='append', default=[], metavar='SOURCE',
@@ -73,7 +79,7 @@ def pytest_addoption(parser):
7379
group.addoption('--no-cov', action='store_true', default=False,
7480
help='Disable coverage report completely (useful for debuggers). '
7581
'Default: False')
76-
group.addoption('--cov-fail-under', action='store', metavar='MIN', type=float,
82+
group.addoption('--cov-fail-under', action='store', metavar='MIN', type=fail_under,
7783
help='Fail if the total coverage is less than MIN.')
7884
group.addoption('--cov-append', action='store_true', default=False,
7985
help='Do not delete coverage but append to current. '
@@ -265,25 +271,19 @@ def pytest_terminal_summary(self, terminalreporter):
265271

266272
terminalreporter.write('\n' + self.cov_report.getvalue() + '\n')
267273

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)
274+
if self.options.cov_fail_under is not None and self.options.cov_fail_under > 0:
275+
failed = self.cov_total < self.options.cov_fail_under
276+
markup = {'red': True, 'bold': True} if failed else {'green': True}
277+
message = (
278+
'{fail}Required test coverage of {required}% {reached}. '
279+
'Total coverage: {actual:.2f}%\n'
280+
.format(
281+
required=self.options.cov_fail_under,
282+
actual=self.cov_total,
283+
fail="FAIL " if failed else "",
284+
reached = "not reached" if failed else "reached"
286285
)
286+
)
287287
terminalreporter.write(message, **markup)
288288

289289
def pytest_runtest_setup(self, item):

0 commit comments

Comments
 (0)