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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
import os
import re
import sys
import optparse
from types import ListType
import coverage
class CoverageTestWrapper(object):
"""
A Coverage Test Wrapper.
1) Setup the with the parsed options
2) Call start()
3) Run your tests
4) Call finish()
5) Improve your code coverage ;)
"""
coverTests = False
coverPackages = None
def __init__(self, options, _covpkg=None):
self.options = options
# _covpkg is for dependency injection, so we can test this code.
if _covpkg:
self.covpkg = _covpkg
else:
import coverage
self.covpkg = coverage
self.coverage = None
self.coverTests = options.cover_tests
self.coverPackage = options.cover_package
def start(self):
# Set up coverage
self.coverage = self.covpkg.coverage(
data_suffix = bool(self.options.cover_parallel_mode),
cover_pylib = self.options.cover_pylib,
timid = self.options.cover_timid,
branch = self.options.cover_branch,
)
self.skipModules = sys.modules.keys()[:]
# Run the script.
self.coverage.start()
def finish(self, stream=None):
# end coverage and save the results
self.coverage.stop()
self.coverage.save()
modules = [module
for name, module in sys.modules.items()
if module is not None and name.startswith(self.coverPackage)]
# Remaining actions are reporting, with some common self.options.
report_args = {
'morfs': modules,
'ignore_errors': self.options.cover_ignore_errors,
}
# Handle any omits
# Allow pointing to a file as well
try:
omit_file = open(self.options.cover_omit)
omit_prefixes = [line.strip() for line in omit_file.readlines()]
report_args['omit_prefixes'] = omit_prefixes
except:
omit = self.options.cover_omit.split(',')
report_args['omit_prefixes'] = omit
if 'report' in self.options.cover_actions:
self.coverage.report(
show_missing=self.options.cover_show_missing,
file=stream, **report_args)
if 'annotate' in self.options.cover_actions:
self.coverage.annotate(
directory=self.options.cover_directory, **report_args)
if 'html' in self.options.cover_actions:
self.coverage.html_report(
directory=self.options.cover_directory, **report_args)
if 'xml' in self.options.cover_actions:
outfile = self.options.cover_outfile
if outfile == '-':
outfile = None
self.coverage.xml_report(outfile=outfile, **report_args)
return
options = [
optparse.Option('',
'--cover-action', action='append', default=None,
dest='cover_actions', type="choice", choices=['annotate', 'html', 'report', 'xml'],
help="""\
annotate Annotate source files with execution information.
html Create an HTML report.
report Report coverage stats on modules.
xml Create an XML report of coverage results.
""".strip()),
optparse.Option(
'--cover-package', action='store',
dest="cover_package",
metavar="COVER_PACKAGE",
help="Restrict coverage output to selected package"
),
optparse.Option("--cover-tests", action="store_true",
dest="cover_tests",
metavar="[NOSE_COVER_TESTS]",
default=False,
help="Include test modules in coverage report "),
optparse.Option(
'--cover-branch', action='store_true',
help="Measure branch execution. HIGHLY EXPERIMENTAL!"
),
optparse.Option(
'--cover-directory', action='store',
metavar="DIR",
help="Write the output files to DIR."
),
optparse.Option(
'--cover-ignore-errors', action='store_true',
help="Ignore errors while reading source files."
),
optparse.Option(
'--cover-pylib', action='store_true',
help="Measure coverage even inside the Python installed library, "
"which isn't done by default."
),
optparse.Option(
'--cover-show-missing', action='store_true',
help="Show line numbers of statements in each module that weren't "
"executed."
),
optparse.Option(
'--cover-omit', action='store',
metavar="PRE1,PRE2,...",
default='',
help="Omit files when their filename path starts with one of these "
"prefixes."
),
optparse.Option(
'--cover-outfile', action='store',
metavar="OUTFILE",
help="Write the XML report to this file. Defaults to 'coverage.xml'"
),
optparse.Option(
'--cover-parallel-mode', action='store_true',
help="Include the machine name and process id in the .coverage "
"data file name."
),
optparse.Option(
'--cover-timid', action='store_true',
help="Use a simpler but slower trace method. Try this if you get "
"seemingly impossible results!"
),
optparse.Option(
'--cover-append', action='store_false',
help="Append coverage data to .coverage, otherwise it is started "
"clean with each run."
)
]
# py.test plugin hooks
def pytest_addoption(parser):
"""
Get all the options from the coverage.runner and import them
"""
group = parser.getgroup('Coverage options')
for opt in options:
group._addoption_instance(opt)
def pytest_configure(config):
# Load the runner and start it up
if config.getvalue("cover_actions"):
config.pluginmanager.register(DoCover(config), "do_coverage")
class DoCover:
def __init__(self, config):
self.config = config
def pytest_sessionstart(self):
self.coverage = CoverageTestWrapper(self.config.option)
# XXX maybe better to start/suspend/resume coverage
# for each single test item
self.coverage.start()
def pytest_terminal_summary(self, terminalreporter):
# Finished the tests start processing the coverage
config = terminalreporter.config
tw = terminalreporter._tw
tw.sep('-', 'coverage')
tw.line('Processing Coverage...')
self.coverage.finish()
# XXX please make the following unnessary
# 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
|