Skip to content

Commit

Permalink
try parsing cov-fail-under as int then float
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Aug 29, 2019
1 parent c6c53c0 commit 8c54284
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/pytest_cov/plugin.py
Expand Up @@ -50,6 +50,12 @@ def __call__(self, parser, namespace, values, option_string=None):
def pytest_addoption(parser):
"""Add options to control coverage."""

def fail_under(num_str):
try:
return int(num_str)
except ValueError:
return float(num_str)

group = parser.getgroup(
'cov', 'coverage reporting with distributed testing support')
group.addoption('--cov', action='append', default=[], metavar='SOURCE',
Expand All @@ -73,7 +79,7 @@ def pytest_addoption(parser):
group.addoption('--no-cov', action='store_true', default=False,
help='Disable coverage report completely (useful for debuggers). '
'Default: False')
group.addoption('--cov-fail-under', action='store', metavar='MIN', type=float,
group.addoption('--cov-fail-under', action='store', metavar='MIN', type=fail_under,
help='Fail if the total coverage is less than MIN.')
group.addoption('--cov-append', action='store_true', default=False,
help='Do not delete coverage but append to current. '
Expand Down Expand Up @@ -265,25 +271,19 @@ def pytest_terminal_summary(self, terminalreporter):

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

fail_under = self.options.cov_fail_under
if fail_under is not None and fail_under > 0:
str_fail_under = str(
round(fail_under, 2) if fail_under % 1 else int(fail_under)
)
if self.cov_total < fail_under:
markup = {'red': True, 'bold': True}
message = (
'FAIL Required test coverage of %s%% not '
'reached. Total coverage: %.2f%%\n'
% (str_fail_under, self.cov_total)
)
else:
markup = {'green': True}
message = (
'Required test coverage of %s%% '
'reached. Total coverage: %.2f%%\n'
% (str_fail_under, self.cov_total)
if self.options.cov_fail_under is not None and self.options.cov_fail_under > 0:
failed = self.cov_total < self.options.cov_fail_under
markup = {'red': True, 'bold': True} if failed else {'green': True}
message = (
'{fail}Required test coverage of {required}% {reached}. '
'Total coverage: {actual:.2f}%\n'
.format(
required=self.options.cov_fail_under,
actual=self.cov_total,
fail="FAIL " if failed else "",
reached = "not reached" if failed else "reached"
)
)
terminalreporter.write(message, **markup)

def pytest_runtest_setup(self, item):
Expand Down

0 comments on commit 8c54284

Please sign in to comment.