From 5a79a8817d5ac303d010468c2a99781502a57f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Sun, 30 Jul 2017 18:48:35 +0300 Subject: [PATCH] Change of behavior: when report generation fails log a warning instead. This is consistent with other kinds of failures (like misconfigration) that result in no coverage data/results. Closes #161. --- src/pytest_cov/plugin.py | 20 +++++++++++--------- tests/test_pytest_cov.py | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index 05ac0393..2ddf61ea 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -238,9 +238,11 @@ 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 @@ -248,9 +250,9 @@ def pytest_runtestloop(self, session): 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 @@ -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: diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 9a39289b..39916532 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -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