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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
"""Tests for process behavior of coverage.py."""
import os, sys, textwrap
import coverage
sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
from coveragetest import CoverageTest
class ProcessTest(CoverageTest):
"""Tests of the per-process behavior of coverage.py."""
def number_of_data_files(self):
"""Return the number of coverage data files in this directory."""
num = 0
for f in os.listdir('.'):
if f.startswith('.coverage.') or f == '.coverage':
num += 1
return num
def test_save_on_exit(self):
self.make_file("mycode.py", """\
h = "Hello"
w = "world"
""")
self.assert_doesnt_exist(".coverage")
self.run_command("coverage -x mycode.py")
self.assert_exists(".coverage")
def test_environment(self):
# Checks that we can import modules from the test directory at all!
self.make_file("mycode.py", """\
import covmod1
import covmodzip1
a = 1
print ('done')
""")
self.assert_doesnt_exist(".coverage")
out = self.run_command("coverage -x mycode.py")
self.assert_exists(".coverage")
self.assertEqual(out, 'done\n')
def test_combine_parallel_data(self):
self.make_file("b_or_c.py", """\
import sys
a = 1
if sys.argv[1] == 'b':
b = 1
else:
c = 1
d = 1
print ('done')
""")
out = self.run_command("coverage -x -p b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
out = self.run_command("coverage -x -p b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
# After two -p runs, there should be two .coverage.machine.123 files.
self.assertEqual(self.number_of_data_files(), 2)
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage -c")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assertEqual(self.number_of_data_files(), 1)
# Read the coverage file and see that b_or_c.py has all 7 lines
# executed.
data = coverage.CoverageData()
data.read_file(".coverage")
self.assertEqual(data.summary()['b_or_c.py'], 7)
def test_combine_with_rc(self):
self.make_file("b_or_c.py", """\
import sys
a = 1
if sys.argv[1] == 'b':
b = 1
else:
c = 1
d = 1
print ('done')
""")
self.make_file(".coveragerc", """\
[run]
parallel = true
""")
out = self.run_command("coverage run b_or_c.py b")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
out = self.run_command("coverage run b_or_c.py c")
self.assertEqual(out, 'done\n')
self.assert_doesnt_exist(".coverage")
# After two runs, there should be two .coverage.machine.123 files.
self.assertEqual(self.number_of_data_files(), 2)
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage combine")
self.assert_exists(".coverage")
self.assert_exists(".coveragerc")
# After combining, there should be only the .coverage file.
self.assertEqual(self.number_of_data_files(), 1)
# Read the coverage file and see that b_or_c.py has all 7 lines
# executed.
data = coverage.CoverageData()
data.read_file(".coverage")
self.assertEqual(data.summary()['b_or_c.py'], 7)
# Reporting should still work even with the .rc file
out = self.run_command("coverage report")
self.assertMultiLineEqual(out, textwrap.dedent("""\
Name Stmts Miss Cover
----------------------------
b_or_c 7 0 100%
"""))
def test_missing_source_file(self):
# Check what happens if the source is missing when reporting happens.
self.make_file("fleeting.py", """\
s = 'goodbye, cruel world!'
""")
self.run_command("coverage run fleeting.py")
os.remove("fleeting.py")
out = self.run_command("coverage html -d htmlcov")
self.assertRegexpMatches(out, "No source for code: '.*fleeting.py'")
self.assertFalse("Traceback" in out)
# It happens that the code paths are different for *.py and other
# files, so try again with no extension.
self.make_file("fleeting", """\
s = 'goodbye, cruel world!'
""")
self.run_command("coverage run fleeting")
os.remove("fleeting")
status, out = self.run_command_status("coverage html -d htmlcov", 1)
self.assertRegexpMatches(out, "No source for code: '.*fleeting'")
self.assertFalse("Traceback" in out)
self.assertEqual(status, 1)
def test_running_missing_file(self):
status, out = self.run_command_status("coverage run xyzzy.py", 1)
self.assertRegexpMatches(out, "No file to run: .*xyzzy.py")
self.assertFalse("Traceback" in out)
self.assertEqual(status, 1)
def test_code_throws(self):
self.make_file("throw.py", """\
def f1():
raise Exception("hey!")
def f2():
f1()
f2()
""")
# The important thing is for "coverage run" and "python" to report the
# same traceback.
status, out = self.run_command_status("coverage run throw.py", 1)
out2 = self.run_command("python throw.py")
self.assertMultiLineEqual(out, out2)
# But also make sure that the output is what we expect.
self.assertTrue('File "throw.py", line 5, in f2' in out)
self.assertTrue('raise Exception("hey!")' in out)
self.assertFalse('coverage' in out)
self.assertEqual(status, 1)
def test_code_exits(self):
self.make_file("exit.py", """\
import sys
def f1():
print("about to exit..")
sys.exit(17)
def f2():
f1()
f2()
""")
# The important thing is for "coverage run" and "python" to have the
# same output. No traceback.
status, out = self.run_command_status("coverage run exit.py", 17)
status2, out2 = self.run_command_status("python exit.py", 17)
self.assertMultiLineEqual(out, out2)
self.assertMultiLineEqual(out, "about to exit..\n")
self.assertEqual(status, status2)
self.assertEqual(status, 17)
def test_code_exits_no_arg(self):
self.make_file("exit_none.py", """\
import sys
def f1():
print("about to exit quietly..")
sys.exit()
f1()
""")
status, out = self.run_command_status("coverage run exit_none.py", 0)
status2, out2 = self.run_command_status("python exit_none.py", 0)
self.assertMultiLineEqual(out, out2)
self.assertMultiLineEqual(out, "about to exit quietly..\n")
self.assertEqual(status, status2)
self.assertEqual(status, 0)
if hasattr(os, 'fork'):
def test_fork(self):
self.make_file("fork.py", """\
import os
def child():
print('Child!')
def main():
ret = os.fork()
if ret == 0:
child()
else:
os.waitpid(ret, 0)
main()
""")
out = self.run_command("coverage run -p fork.py")
self.assertEqual(out, 'Child!\n')
self.assert_doesnt_exist(".coverage")
# After running the forking program, there should be two
# .coverage.machine.123 files.
self.assertEqual(self.number_of_data_files(), 2)
# Combine the parallel coverage data files into .coverage .
self.run_command("coverage -c")
self.assert_exists(".coverage")
# After combining, there should be only the .coverage file.
self.assertEqual(self.number_of_data_files(), 1)
# Read the coverage file and see that b_or_c.py has all 7 lines
# executed.
data = coverage.CoverageData()
data.read_file(".coverage")
self.assertEqual(data.summary()['fork.py'], 9)
def test_warnings(self):
self.make_file("hello.py", """\
import sys, os
print("Hello")
""")
out = self.run_command("coverage run --source=sys,xyzzy,quux hello.py")
self.assertTrue("Hello\n" in out)
self.assertTrue(textwrap.dedent("""\
Coverage.py warning: Module sys has no python source.
Coverage.py warning: Module xyzzy was never imported.
Coverage.py warning: Module quux was never imported.
Coverage.py warning: No data was collected.
""") in out)
def test_warnings_if_never_run(self):
out = self.run_command("coverage run i_dont_exist.py")
self.assertTrue("No file to run: 'i_dont_exist.py'" in out)
self.assertTrue("warning" not in out)
out = self.run_command("coverage run -m no_such_module")
self.assertTrue("No module named no_such_module" in out)
self.assertTrue("warning" not in out)
|