Skip to content

Commit 1734d2d

Browse files
graingertionelmc
authored andcommitted
warn specific classes
1 parent 87a1173 commit 1734d2d

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

src/pytest_cov/compat.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import warnings
2+
3+
import pytest
4+
15
try:
26
from StringIO import StringIO
37
except ImportError:
48
from io import StringIO
59

6-
import pytest
7-
810
StringIO # pyflakes, this is for re-export
911

12+
PYTEST_VERSION = tuple(int(v) for v in pytest.__version__.split('.')[:3])
1013

1114
if hasattr(pytest, 'hookimpl'):
1215
hookwrapper = pytest.hookimpl(hookwrapper=True)
@@ -31,6 +34,21 @@ def testsfailed(self, value):
3134
setattr(self._session, self._attr, value)
3235

3336

37+
if PYTEST_VERSION >= (3, 8):
38+
def warn(config, message, category, stacklevel=1):
39+
return warnings.warn(
40+
message=message,
41+
category=category,
42+
stacklevel=stacklevel+1,
43+
)
44+
else:
45+
def warn(config, message, category, stacklevel=1):
46+
return config.warn(
47+
code=category.code,
48+
message=message,
49+
)
50+
51+
3452
def _attrgetter(attr):
3553
"""
3654
Return a callable object that fetches attr from its operand.
@@ -43,7 +61,6 @@ def fn(obj, *args):
4361

4462
return fn
4563

46-
4764
worker = 'slave' # for compatability with pytest-xdist<=1.22.0
4865
workerid = worker + 'id'
4966
workerinput = _attrgetter(worker + 'input')

src/pytest_cov/plugin.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
"""Coverage plugin for pytest."""
22
import argparse
33
import os
4-
import warnings
54

65
import coverage
76
import pytest
87

98
from . import compat
109
from . import embed
1110

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

1512
class CoverageError(Exception):
1613
"""Indicates that our coverage is too low"""
1714

1815

16+
class PytestCovWarning(pytest.PytestWarning):
17+
"""
18+
The base for all pytest-cov warnings, never raised directly
19+
"""
20+
code = "COV-0"
21+
22+
23+
class CovDisabledWarning(PytestCovWarning):
24+
"""Indicates that Coverage was manually disabled"""
25+
code = "COV-1"
26+
27+
28+
class CovReportWarning(PytestCovWarning):
29+
"""Indicates that we failed to generate a report"""
30+
code = "COV-2"
31+
32+
1933
def validate_report(arg):
2034
file_choices = ['annotate', 'html', 'xml']
2135
term_choices = ['term', 'term-missing']
@@ -273,10 +287,11 @@ def pytest_runtestloop(self, session):
273287
message = 'Failed to generate report: %s\n' % exc
274288
session.config.pluginmanager.getplugin("terminalreporter").write(
275289
'WARNING: %s\n' % message, red=True, bold=True)
276-
if PYTEST_VERSION >= (3, 8):
277-
warnings.warn(pytest.PytestWarning(message))
278-
else:
279-
session.config.warn(code='COV-2', message=message)
290+
compat.warn(
291+
config=session.config,
292+
message=message,
293+
category=CovReportWarning,
294+
)
280295
self.cov_total = 0
281296
assert self.cov_total is not None, 'Test coverage should never be `None`'
282297
if self._failed_cov_total():
@@ -287,10 +302,11 @@ def pytest_terminal_summary(self, terminalreporter):
287302
if self._disabled:
288303
message = 'Coverage disabled via --no-cov switch!'
289304
terminalreporter.write('WARNING: %s\n' % message, red=True, bold=True)
290-
if PYTEST_VERSION >= (3, 8):
291-
warnings.warn(pytest.PytestWarning(message))
292-
else:
293-
terminalreporter.config.warn(code='COV-1', message=message)
305+
compat.warn(
306+
config=terminalreporter.config,
307+
message=message,
308+
category=CovDisabledWarning,
309+
)
294310
return
295311
if self.cov_controller is None:
296312
return

0 commit comments

Comments
 (0)