Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support float values for --cov-fail-under. #311

Merged
merged 4 commits into from Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/pytest_cov/plugin.py
Expand Up @@ -41,6 +41,13 @@ def validate_report(arg):
return values


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


class StoreReport(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
report_type, file = values
Expand Down Expand Up @@ -73,7 +80,8 @@ 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=int,
group.addoption('--cov-fail-under', action='store', metavar='MIN',
type=validate_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 @@ -266,20 +274,18 @@ def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write('\n' + self.cov_report.getvalue() + '\n')

if self.options.cov_fail_under is not None and self.options.cov_fail_under > 0:
if self.cov_total < self.options.cov_fail_under:
markup = {'red': True, 'bold': True}
message = (
'FAIL Required test coverage of %d%% not '
'reached. Total coverage: %.2f%%\n'
% (self.options.cov_fail_under, self.cov_total)
)
else:
markup = {'green': True}
message = (
'Required test coverage of %d%% '
'reached. Total coverage: %.2f%%\n'
% (self.options.cov_fail_under, self.cov_total)
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
28 changes: 28 additions & 0 deletions tests/test_pytest_cov.py
Expand Up @@ -343,6 +343,34 @@ def test_cov_min_50(testdir):
])


def test_cov_min_float_value(testdir):
script = testdir.makepyfile(SCRIPT)
graingert marked this conversation as resolved.
Show resolved Hide resolved

result = testdir.runpytest('-v',
'--cov=%s' % script.dirpath(),
'--cov-report=term-missing',
'--cov-fail-under=88.88',
script)
assert result.ret == 0
result.stdout.fnmatch_lines([
'Required test coverage of 88.88% reached. Total coverage: 88.89%'
])


def test_cov_min_float_value_not_reached(testdir):
script = testdir.makepyfile(SCRIPT)

result = testdir.runpytest('-v',
'--cov=%s' % script.dirpath(),
'--cov-report=term-missing',
'--cov-fail-under=88.89',
script)
assert result.ret == 1
result.stdout.fnmatch_lines([
'FAIL Required test coverage of 88.89% not reached. Total coverage: 88.89%'
])


def test_cov_min_no_report(testdir):
script = testdir.makepyfile(SCRIPT)

Expand Down