1
1
"""Coverage plugin for pytest."""
2
2
import argparse
3
3
import os
4
- import warnings
5
4
6
5
import coverage
7
6
import pytest
11
10
from . import embed
12
11
from . import engine
13
12
14
- PYTEST_VERSION = tuple (map (int , pytest .__version__ .split ('.' )[:3 ]))
15
-
16
13
17
14
class CoverageError (Exception ):
18
15
"""Indicates that our coverage is too low"""
19
16
20
17
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
+
21
35
def validate_report (arg ):
22
36
file_choices = ['annotate' , 'html' , 'xml' ]
23
37
term_choices = ['term' , 'term-missing' ]
@@ -262,10 +276,11 @@ def pytest_runtestloop(self, session):
262
276
message = 'Failed to generate report: %s\n ' % exc
263
277
session .config .pluginmanager .getplugin ("terminalreporter" ).write (
264
278
'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
+ )
269
284
self .cov_total = 0
270
285
assert self .cov_total is not None , 'Test coverage should never be `None`'
271
286
if self ._failed_cov_total ():
@@ -276,10 +291,11 @@ def pytest_terminal_summary(self, terminalreporter):
276
291
if self ._disabled :
277
292
message = 'Coverage disabled via --no-cov switch!'
278
293
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
+ )
283
299
return
284
300
if self .cov_controller is None :
285
301
return
0 commit comments