Skip to content

Commit 22c4ae5

Browse files
tsvikasionelmc
authored andcommitted
move heading to pytest_terminal_summary
1 parent 82d5ea0 commit 22c4ae5

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/pytest_cov/plugin.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
from coverage.results import display_covered
1212
from coverage.results import should_fail_under
1313

14-
try:
15-
from pytest import TerminalReporter # noqa: PT013
16-
except ImportError:
17-
from _pytest.terminal import TerminalReporter
18-
1914
from . import CovDisabledWarning
2015
from . import CovFailUnderWarning
2116
from . import CovReportWarning
@@ -222,6 +217,7 @@ def __init__(self, options, pluginmanager, start=True, no_cov_should_warn=False)
222217
self._start_path = None
223218
self._disabled = False
224219
self.options = options
220+
self.wrote_heading = False
225221

226222
is_dist = getattr(options, 'numprocesses', False) or getattr(options, 'distload', False) or getattr(options, 'dist', 'no') != 'no'
227223
if getattr(options, 'no_cov', False):
@@ -338,8 +334,6 @@ def pytest_runtestloop(self, session):
338334
# it for unit tests that don't need it
339335
from coverage.misc import CoverageException
340336

341-
tr = TerminalReporter(session.config, self.cov_report)
342-
tr.write_sep('=', 'coverage report')
343337
try:
344338
self.cov_total = self.cov_controller.summary(self.cov_report)
345339
except CoverageException as exc:
@@ -363,9 +357,15 @@ def pytest_runtestloop(self, session):
363357
# make sure we get the EXIT_TESTSFAILED exit code
364358
compat_session.testsfailed += 1
365359

360+
def write_heading(self, terminalreporter):
361+
if not self.wrote_heading:
362+
terminalreporter.write_sep('=', 'tests coverage')
363+
self.wrote_heading = True
364+
366365
def pytest_terminal_summary(self, terminalreporter):
367366
if self._disabled:
368367
if self.options.no_cov_should_warn:
368+
self.write_heading(terminalreporter)
369369
message = 'Coverage disabled via --no-cov switch!'
370370
terminalreporter.write(f'WARNING: {message}\n', red=True, bold=True)
371371
warnings.warn(CovDisabledWarning(message), stacklevel=1)
@@ -381,9 +381,11 @@ def pytest_terminal_summary(self, terminalreporter):
381381

382382
# Avoid undesirable new lines when output is disabled with "--cov-report=".
383383
if report:
384+
self.write_heading(terminalreporter)
384385
terminalreporter.write('\n' + report + '\n')
385386

386387
if self.options.cov_fail_under is not None and self.options.cov_fail_under > 0:
388+
self.write_heading(terminalreporter)
387389
failed = self.cov_total < self.options.cov_fail_under
388390
markup = {'red': True, 'bold': True} if failed else {'green': True}
389391
message = '{fail}Required test coverage of {required}% {reached}. ' 'Total coverage: {actual:.2f}%\n'.format(

0 commit comments

Comments
 (0)