Skip to content

Commit fe580ae

Browse files
committed
warn specific classes
1 parent 090e2ca commit fe580ae

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/pytest_cov/compat.py

Lines changed: 20 additions & 2 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)
@@ -44,6 +47,21 @@ def fn(obj, *args):
4447
return fn
4548

4649

50+
if PYTEST_VERSION >= (3, 8):
51+
def warn(config, message, category, stacklevel=1):
52+
return warnings.warn(
53+
message=message,
54+
category=category,
55+
stacklevel=stacklevel+1,
56+
)
57+
else:
58+
def warn(config, message, category, stacklevel=1):
59+
return config.warn(
60+
code=category.code,
61+
message=message,
62+
)
63+
64+
4765
worker = 'slave' # for compatability with pytest-xdist<=1.22.0
4866
workerid = worker + 'id'
4967
workerinput = _attrgetter(worker + 'input')

src/pytest_cov/plugin.py

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

65
import coverage
76
import pytest
@@ -11,13 +10,28 @@
1110
from . import embed
1211
from . import engine
1312

14-
PYTEST_VERSION = tuple(map(int, pytest.__version__.split('.')[:3]))
15-
1613

1714
class CoverageError(Exception):
1815
"""Indicates that our coverage is too low"""
1916

2017

18+
class PytestCovWarning(pytest.PytestWarning):
19+
"""
20+
The base for all pytest-cov warnings, never raised directly
21+
"""
22+
code = "COV-0"
23+
24+
25+
class CovDisabledWarning(PytestCovWarning):
26+
"""Indicates that Coverage was manually disabled"""
27+
code = "COV-1"
28+
29+
30+
class CovReportWarning(PytestCovWarning):
31+
"""Indicates that we failed to generate a report"""
32+
code = "COV-2"
33+
34+
2135
def validate_report(arg):
2236
file_choices = ['annotate', 'html', 'xml']
2337
term_choices = ['term', 'term-missing']
@@ -262,10 +276,11 @@ def pytest_runtestloop(self, session):
262276
message = 'Failed to generate report: %s\n' % exc
263277
session.config.pluginmanager.getplugin("terminalreporter").write(
264278
'WARNING: %s\n' % message, red=True, bold=True)
265-
if PYTEST_VERSION >= (3, 8):
266-
warnings.warn(pytest.PytestWarning(message))
267-
else:
268-
session.config.warn(code='COV-2', message=message)
279+
compat.warn(
280+
config=session.config,
281+
message=message,
282+
category=CovReportWarning,
283+
)
269284
self.cov_total = 0
270285
assert self.cov_total is not None, 'Test coverage should never be `None`'
271286
if self._failed_cov_total():
@@ -276,10 +291,11 @@ def pytest_terminal_summary(self, terminalreporter):
276291
if self._disabled:
277292
message = 'Coverage disabled via --no-cov switch!'
278293
terminalreporter.write('WARNING: %s\n' % message, red=True, bold=True)
279-
if PYTEST_VERSION >= (3, 8):
280-
warnings.warn(pytest.PytestWarning(message))
281-
else:
282-
terminalreporter.config.warn(code='COV-1', message=message)
294+
compat.warn(
295+
config=terminalreporter.config,
296+
message=message,
297+
category=CovDisabledWarning,
298+
)
283299
return
284300
if self.cov_controller is None:
285301
return

0 commit comments

Comments
 (0)