From 8c54284fc7f700c5ca03957ff1a6c5e3f9e223c7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 29 Aug 2019 14:54:12 +0100 Subject: [PATCH] try parsing cov-fail-under as int then float --- src/pytest_cov/plugin.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index f1955088..a5ba748e 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -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', @@ -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. ' @@ -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):