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
8
7
9
8
from . import compat
10
9
from . import embed
11
10
12
- PYTEST_VERSION = tuple (map (int , pytest .__version__ .split ('.' )[:3 ]))
13
-
14
11
15
12
class CoverageError (Exception ):
16
13
"""Indicates that our coverage is too low"""
17
14
18
15
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
+
19
33
def validate_report (arg ):
20
34
file_choices = ['annotate' , 'html' , 'xml' ]
21
35
term_choices = ['term' , 'term-missing' ]
@@ -273,10 +287,11 @@ def pytest_runtestloop(self, session):
273
287
message = 'Failed to generate report: %s\n ' % exc
274
288
session .config .pluginmanager .getplugin ("terminalreporter" ).write (
275
289
'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
+ )
280
295
self .cov_total = 0
281
296
assert self .cov_total is not None , 'Test coverage should never be `None`'
282
297
if self ._failed_cov_total ():
@@ -287,10 +302,11 @@ def pytest_terminal_summary(self, terminalreporter):
287
302
if self ._disabled :
288
303
message = 'Coverage disabled via --no-cov switch!'
289
304
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
+ )
294
310
return
295
311
if self .cov_controller is None :
296
312
return
0 commit comments