summaryrefslogtreecommitdiff
path: root/coverage/test_plugins/pytest_coverage.py
blob: 5b910e949d5a186c44bcd9080c90be769e4b5861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Write and report coverage data with 'coverage.py'. 
"""
import py

coverage = py.test.importorskip("coverage")

def pytest_configure(config):
    # Load the runner and start it up
    from coverage.runner import CoverageTestWrapper
    
    config.coverage = CoverageTestWrapper(config.option)
    config.coverage.start()

def pytest_terminal_summary(terminalreporter):
    # Finished the tests start processing the coverage
    config = terminalreporter.config
    tw = terminalreporter._tw
    tw.sep('-', 'coverage')
    tw.line('Processing Coverage...')
    
    # finish up with coverage
    config.coverage.finish()

def pytest_addoption(parser):
    """
    Get all the options from the coverage.runner and import them
    """
    from coverage.runner import Options
    
    group = parser.getgroup('Coverage options')
    # Loop the coverage options and append them to the plugin options
    options = [a for a in dir(Options) if not a.startswith('_')]
    for option in options:
        opt = getattr(Options, option)
        group._addoption_instance(opt, shortupper=True)

# Monkey patch omit_filter to use regex patterns for file omits
def omit_filter(omit_prefixes, code_units):
    import re
    exclude_patterns = [re.compile(line.strip()) for line in omit_prefixes if line and not line.startswith('#')]
    filtered = []
    for cu in code_units:
        skip = False
        for pattern in exclude_patterns:
            if pattern.search(cu.filename):
                skip = True
                break
            
        if not skip:
            filtered.append(cu)
    return filtered

coverage.codeunit.omit_filter = omit_filter

def test_functional(testdir):
    py.test.importorskip("coverage")
    testdir.plugins.append("coverage")
    testdir.makepyfile("""
        def f():    
            x = 42
        def test_whatever():
            pass
        """)
    result = testdir.runpytest()
    assert result.ret == 0
    assert result.stdout.fnmatch_lines([
        '*Processing Coverage*'
        ])