Skip to content

Commit 1689c9a

Browse files
committed
Implement better --no-cov warning: only do it if --no-cov is present before --cov. Also remove some legacy pytest support and bump min version in setup.py. Closes #407. Ref #304.
1 parent c5623ec commit 1689c9a

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def run(self):
122122
'cover', 'coverage', 'pytest', 'py.test', 'distributed', 'parallel',
123123
],
124124
install_requires=[
125-
'pytest>=3.6',
125+
'pytest>=4.6',
126126
'coverage>=4.4'
127127
],
128128
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*',

src/pytest_cov/plugin.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from . import compat
1010
from . import embed
1111

12-
PYTEST_VERSION = tuple(map(int, pytest.__version__.split('.')[:3]))
13-
1412

1513
class CoverageError(Exception):
1614
"""Indicates that our coverage is too low"""
@@ -114,8 +112,17 @@ def _prepare_cov_source(cov_source):
114112

115113
@pytest.mark.tryfirst
116114
def pytest_load_initial_conftests(early_config, parser, args):
115+
options = early_config.known_args_namespace
116+
no_cov = options.no_cov_should_warn = False
117+
for arg in args:
118+
if arg == '--no-cov':
119+
no_cov = True
120+
elif arg.startswith('--cov') and no_cov:
121+
options.no_cov_should_warn = True
122+
break
123+
117124
if early_config.known_args_namespace.cov_source:
118-
plugin = CovPlugin(early_config.known_args_namespace, early_config.pluginmanager)
125+
plugin = CovPlugin(options, early_config.pluginmanager)
119126
early_config.pluginmanager.register(plugin, '_cov')
120127

121128

@@ -127,7 +134,7 @@ class CovPlugin(object):
127134
distributed worker.
128135
"""
129136

130-
def __init__(self, options, pluginmanager, start=True):
137+
def __init__(self, options, pluginmanager, start=True, no_cov_should_warn=False):
131138
"""Creates a coverage pytest plugin.
132139
133140
We read the rc file that coverage uses to get the data file
@@ -275,10 +282,7 @@ def pytest_runtestloop(self, session):
275282
message = 'Failed to generate report: %s\n' % exc
276283
session.config.pluginmanager.getplugin("terminalreporter").write(
277284
'WARNING: %s\n' % message, red=True, bold=True)
278-
if PYTEST_VERSION >= (3, 8):
279-
warnings.warn(pytest.PytestWarning(message))
280-
else:
281-
session.config.warn(code='COV-2', message=message)
285+
warnings.warn(pytest.PytestWarning(message))
282286
self.cov_total = 0
283287
assert self.cov_total is not None, 'Test coverage should never be `None`'
284288
if self._failed_cov_total():
@@ -287,12 +291,10 @@ def pytest_runtestloop(self, session):
287291

288292
def pytest_terminal_summary(self, terminalreporter):
289293
if self._disabled:
290-
message = 'Coverage disabled via --no-cov switch!'
291-
terminalreporter.write('WARNING: %s\n' % message, red=True, bold=True)
292-
if PYTEST_VERSION >= (3, 8):
294+
if self.options.no_cov_should_warn:
295+
message = 'Coverage disabled via --no-cov switch!'
296+
terminalreporter.write('WARNING: %s\n' % message, red=True, bold=True)
293297
warnings.warn(pytest.PytestWarning(message))
294-
else:
295-
terminalreporter.config.warn(code='COV-1', message=message)
296298
return
297299
if self.cov_controller is None:
298300
return

tests/test_pytest_cov.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,15 @@ def test_fail():
666666
result.stdout.fnmatch_lines(['*1 failed*'])
667667

668668

669-
def test_no_cov(testdir):
669+
def test_no_cov(testdir, monkeypatch):
670670
script = testdir.makepyfile(SCRIPT)
671-
671+
testdir.makeini("""
672+
[pytest]
673+
addopts=--no-cov
674+
""")
672675
result = testdir.runpytest('-vvv',
673676
'--cov=%s' % script.dirpath(),
674677
'--cov-report=term-missing',
675-
'--no-cov',
676678
'-rw',
677679
script)
678680
result.stdout.fnmatch_lines_random([
@@ -2009,8 +2011,9 @@ def test_cov_and_no_cov(testdir):
20092011
result = testdir.runpytest('-v',
20102012
'--cov', '--no-cov',
20112013
'-n', '1',
2014+
'-s',
20122015
script)
2013-
2016+
result.stdout.no_fnmatch_line('*Coverage disabled via --no-cov switch!*')
20142017
assert result.ret == 0
20152018

20162019

0 commit comments

Comments
 (0)