Skip to content

Commit

Permalink
Change of behavior: when report generation fails log a warning instea…
Browse files Browse the repository at this point in the history
…d. This is consistent with other kinds of failures (like misconfigration) that result in no coverage data/results. Closes #161.
  • Loading branch information
ionelmc committed Jul 30, 2017
1 parent 2abacc5 commit 5a79a88
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,21 @@ def pytest_runtestloop(self, session):
try:
self.cov_total = self.cov_controller.summary(self.cov_report)
except CoverageException as exc:
raise pytest.UsageError(
'Failed to generate report: %s\n' % exc
)
message = 'Failed to generate report: %s\n' % exc
session.config.pluginmanager.getplugin("terminalreporter").write(
'WARNING: %s\n' % message, red=True, bold=True)
session.config.warn(code='COV-2', message=message)
self.cov_total = 0
assert self.cov_total is not None, 'Test coverage should never be `None`'
if self._failed_cov_total():
# make sure we get the EXIT_TESTSFAILED exit code
compat_session.testsfailed += 1

def pytest_terminal_summary(self, terminalreporter):
if self._disabled:
msg = 'Coverage disabled via --no-cov switch!'
terminalreporter.write('WARNING: %s\n' % msg, red=True, bold=True)
terminalreporter.config.warn(code='COV-U1', message=msg)
message = 'Coverage disabled via --no-cov switch!'
terminalreporter.write('WARNING: %s\n' % message, red=True, bold=True)
terminalreporter.config.warn(code='COV-1', message=message)
return
if self.cov_controller is None:
return
Expand All @@ -264,19 +266,19 @@ def pytest_terminal_summary(self, terminalreporter):
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}
msg = (
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}
msg = (
message = (
'Required test coverage of %d%% '
'reached. Total coverage: %.2f%%\n'
% (self.options.cov_fail_under, self.cov_total)
)
terminalreporter.write(msg, **markup)
terminalreporter.write(message, **markup)

def pytest_runtest_setup(self, item):
if os.getpid() != self.pid:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,8 @@ def test_invalid_coverage_source(testdir):
assert result.ret == 0
else:
# newer `coverage report` errors on missing imports
result.stderr.fnmatch_lines([
'ERROR: Failed to generate report: No data to report.',
result.stdout.fnmatch_lines([
'*Failed to generate report: No data to report.',
])
assert result.ret != 0

Expand Down

0 comments on commit 5a79a88

Please sign in to comment.