blob: 33d7aa14693af424f3881ffadbe43928767b9dc6 [file] [log] [blame]
Alexander Belopolsky4d770172010-09-13 18:14:34 +00001import os
2import sys
3from test.support import (run_unittest, TESTFN, rmtree, unlink,
4 captured_stdout)
Georg Brandl283b1252010-08-02 12:48:46 +00005import unittest
Georg Brandl283b1252010-08-02 12:48:46 +00006
Alexander Belopolsky4d770172010-09-13 18:14:34 +00007import trace
8from trace import CoverageResults, Trace
9
10from test.tracedmodules import testmod
11
12
13#------------------------------- Utilities -----------------------------------#
14
15def fix_ext_py(filename):
16 """Given a .pyc/.pyo filename converts it to the appropriate .py"""
17 if filename.endswith(('.pyc', '.pyo')):
18 filename = filename[:-1]
19 return filename
20
21def my_file_and_modname():
22 """The .py file and module name of this file (__file__)"""
23 modname = os.path.splitext(os.path.basename(__file__))[0]
24 return fix_ext_py(__file__), modname
25
26def get_firstlineno(func):
27 return func.__code__.co_firstlineno
28
29#-------------------- Target functions for tracing ---------------------------#
30#
31# The relative line numbers of lines in these functions matter for verifying
32# tracing. Please modify the appropriate tests if you change one of the
33# functions. Absolute line numbers don't matter.
34#
35
36def traced_func_linear(x, y):
37 a = x
38 b = y
39 c = a + b
40 return c
41
42def traced_func_loop(x, y):
43 c = x
44 for i in range(5):
45 c += y
46 return c
47
48def traced_func_importing(x, y):
49 return x + y + testmod.func(1)
50
51def traced_func_simple_caller(x):
52 c = traced_func_linear(x, x)
53 return c + x
54
55def traced_func_importing_caller(x):
56 k = traced_func_simple_caller(x)
57 k += traced_func_importing(k, x)
58 return k
59
60def traced_func_generator(num):
61 c = 5 # executed once
62 for i in range(num):
63 yield i + c
64
65def traced_func_calling_generator():
66 k = 0
67 for i in traced_func_generator(10):
68 k += i
69
70def traced_doubler(num):
71 return num * 2
72
73def traced_caller_list_comprehension():
74 k = 10
75 mylist = [traced_doubler(i) for i in range(k)]
76 return mylist
77
78
79class TracedClass(object):
80 def __init__(self, x):
81 self.a = x
82
83 def inst_method_linear(self, y):
84 return self.a + y
85
86 def inst_method_calling(self, x):
87 c = self.inst_method_linear(x)
88 return c + traced_func_linear(x, c)
89
90 @classmethod
91 def class_method_linear(cls, y):
92 return y * 2
93
94 @staticmethod
95 def static_method_linear(y):
96 return y * 2
97
98
99#------------------------------ Test cases -----------------------------------#
100
101
102class TestLineCounts(unittest.TestCase):
103 """White-box testing of line-counting, via runfunc"""
104 def setUp(self):
105 self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
106 self.my_py_filename = fix_ext_py(__file__)
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000107
108 def test_traced_func_linear(self):
109 result = self.tracer.runfunc(traced_func_linear, 2, 5)
110 self.assertEqual(result, 7)
111
112 # all lines are executed once
113 expected = {}
114 firstlineno = get_firstlineno(traced_func_linear)
115 for i in range(1, 5):
116 expected[(self.my_py_filename, firstlineno + i)] = 1
117
118 self.assertEqual(self.tracer.results().counts, expected)
119
120 def test_traced_func_loop(self):
121 self.tracer.runfunc(traced_func_loop, 2, 3)
122
123 firstlineno = get_firstlineno(traced_func_loop)
124 expected = {
125 (self.my_py_filename, firstlineno + 1): 1,
126 (self.my_py_filename, firstlineno + 2): 6,
127 (self.my_py_filename, firstlineno + 3): 5,
128 (self.my_py_filename, firstlineno + 4): 1,
129 }
130 self.assertEqual(self.tracer.results().counts, expected)
131
132 def test_traced_func_importing(self):
133 self.tracer.runfunc(traced_func_importing, 2, 5)
134
135 firstlineno = get_firstlineno(traced_func_importing)
136 expected = {
137 (self.my_py_filename, firstlineno + 1): 1,
138 (fix_ext_py(testmod.__file__), 2): 1,
139 (fix_ext_py(testmod.__file__), 3): 1,
140 }
141
142 self.assertEqual(self.tracer.results().counts, expected)
143
144 def test_trace_func_generator(self):
145 self.tracer.runfunc(traced_func_calling_generator)
146
147 firstlineno_calling = get_firstlineno(traced_func_calling_generator)
148 firstlineno_gen = get_firstlineno(traced_func_generator)
149 expected = {
150 (self.my_py_filename, firstlineno_calling + 1): 1,
151 (self.my_py_filename, firstlineno_calling + 2): 11,
152 (self.my_py_filename, firstlineno_calling + 3): 10,
153 (self.my_py_filename, firstlineno_gen + 1): 1,
154 (self.my_py_filename, firstlineno_gen + 2): 11,
155 (self.my_py_filename, firstlineno_gen + 3): 10,
156 }
157 self.assertEqual(self.tracer.results().counts, expected)
158
159 def test_trace_list_comprehension(self):
160 self.tracer.runfunc(traced_caller_list_comprehension)
161
162 firstlineno_calling = get_firstlineno(traced_caller_list_comprehension)
163 firstlineno_called = get_firstlineno(traced_doubler)
164 expected = {
165 (self.my_py_filename, firstlineno_calling + 1): 1,
166 # List compehentions work differently in 3.x, so the count
167 # below changed compared to 2.x.
168 (self.my_py_filename, firstlineno_calling + 2): 12,
169 (self.my_py_filename, firstlineno_calling + 3): 1,
170 (self.my_py_filename, firstlineno_called + 1): 10,
171 }
172 self.assertEqual(self.tracer.results().counts, expected)
173
174
175 def test_linear_methods(self):
176 # XXX todo: later add 'static_method_linear' and 'class_method_linear'
177 # here, once issue1764286 is resolved
178 #
179 for methname in ['inst_method_linear',]:
180 tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
181 traced_obj = TracedClass(25)
182 method = getattr(traced_obj, methname)
183 tracer.runfunc(method, 20)
184
185 firstlineno = get_firstlineno(method)
186 expected = {
187 (self.my_py_filename, firstlineno + 1): 1,
188 }
189 self.assertEqual(tracer.results().counts, expected)
190
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000191class TestRunExecCounts(unittest.TestCase):
192 """A simple sanity test of line-counting, via runctx (exec)"""
193 def setUp(self):
194 self.my_py_filename = fix_ext_py(__file__)
195
196 def test_exec_counts(self):
197 self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0)
198 code = r'''traced_func_loop(2, 5)'''
199 code = compile(code, __file__, 'exec')
200 self.tracer.runctx(code, globals(), vars())
201
202 firstlineno = get_firstlineno(traced_func_loop)
203 expected = {
204 (self.my_py_filename, firstlineno + 1): 1,
205 (self.my_py_filename, firstlineno + 2): 6,
206 (self.my_py_filename, firstlineno + 3): 5,
207 (self.my_py_filename, firstlineno + 4): 1,
208 }
209
210 # When used through 'run', some other spurios counts are produced, like
211 # the settrace of threading, which we ignore, just making sure that the
212 # counts fo traced_func_loop were right.
213 #
214 for k in expected.keys():
215 self.assertEqual(self.tracer.results().counts[k], expected[k])
216
217
218class TestFuncs(unittest.TestCase):
219 """White-box testing of funcs tracing"""
220 def setUp(self):
221 self.tracer = Trace(count=0, trace=0, countfuncs=1)
222 self.filemod = my_file_and_modname()
223
224 def test_simple_caller(self):
225 self.tracer.runfunc(traced_func_simple_caller, 1)
226
227 expected = {
228 self.filemod + ('traced_func_simple_caller',): 1,
229 self.filemod + ('traced_func_linear',): 1,
230 }
231 self.assertEqual(self.tracer.results().calledfuncs, expected)
232
233 def test_loop_caller_importing(self):
234 self.tracer.runfunc(traced_func_importing_caller, 1)
235
236 expected = {
237 self.filemod + ('traced_func_simple_caller',): 1,
238 self.filemod + ('traced_func_linear',): 1,
239 self.filemod + ('traced_func_importing_caller',): 1,
240 self.filemod + ('traced_func_importing',): 1,
241 (fix_ext_py(testmod.__file__), 'testmod', 'func'): 1,
242 }
243 self.assertEqual(self.tracer.results().calledfuncs, expected)
244
245 def test_inst_method_calling(self):
246 obj = TracedClass(20)
247 self.tracer.runfunc(obj.inst_method_calling, 1)
248
249 expected = {
250 self.filemod + ('TracedClass.inst_method_calling',): 1,
251 self.filemod + ('TracedClass.inst_method_linear',): 1,
252 self.filemod + ('traced_func_linear',): 1,
253 }
254 self.assertEqual(self.tracer.results().calledfuncs, expected)
255
256
257class TestCallers(unittest.TestCase):
258 """White-box testing of callers tracing"""
259 def setUp(self):
260 self.tracer = Trace(count=0, trace=0, countcallers=1)
261 self.filemod = my_file_and_modname()
262
263 def test_loop_caller_importing(self):
264 self.tracer.runfunc(traced_func_importing_caller, 1)
265
266 expected = {
267 ((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'),
268 (self.filemod + ('traced_func_importing_caller',))): 1,
269 ((self.filemod + ('traced_func_simple_caller',)),
270 (self.filemod + ('traced_func_linear',))): 1,
271 ((self.filemod + ('traced_func_importing_caller',)),
272 (self.filemod + ('traced_func_simple_caller',))): 1,
273 ((self.filemod + ('traced_func_importing_caller',)),
274 (self.filemod + ('traced_func_importing',))): 1,
275 ((self.filemod + ('traced_func_importing',)),
276 (fix_ext_py(testmod.__file__), 'testmod', 'func')): 1,
277 }
278 self.assertEqual(self.tracer.results().callers, expected)
279
280
281# Created separately for issue #3821
Georg Brandl283b1252010-08-02 12:48:46 +0000282class TestCoverage(unittest.TestCase):
283 def tearDown(self):
284 rmtree(TESTFN)
285 unlink(TESTFN)
286
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000287 def _coverage(self, tracer,
288 cmd='from test import test_pprint; test_pprint.test_main()'):
289 tracer.run(cmd)
Georg Brandl283b1252010-08-02 12:48:46 +0000290 r = tracer.results()
291 r.write_results(show_missing=True, summary=True, coverdir=TESTFN)
292
293 def test_coverage(self):
294 tracer = trace.Trace(trace=0, count=1)
295 with captured_stdout() as stdout:
296 self._coverage(tracer)
297 stdout = stdout.getvalue()
298 self.assertTrue("pprint.py" in stdout)
299 self.assertTrue("case.py" in stdout) # from unittest
300 files = os.listdir(TESTFN)
301 self.assertTrue("pprint.cover" in files)
302 self.assertTrue("unittest.case.cover" in files)
303
304 def test_coverage_ignore(self):
305 # Ignore all files, nothing should be traced nor printed
306 libpath = os.path.normpath(os.path.dirname(os.__file__))
307 # sys.prefix does not work when running from a checkout
308 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix, libpath],
309 trace=0, count=1)
310 with captured_stdout() as stdout:
311 self._coverage(tracer)
Georg Brandl283b1252010-08-02 12:48:46 +0000312 if os.path.exists(TESTFN):
313 files = os.listdir(TESTFN)
314 self.assertEquals(files, [])
315
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000316 def test_issue9936(self):
317 tracer = trace.Trace(trace=0, count=1)
318 modname = 'test.tracedmodules.testmod'
319 # Ensure that the module is executed in import
320 if modname in sys.modules:
321 del sys.modules[modname]
322 cmd = ("import test.tracedmodules.testmod as t;"
323 "t.func(0); t.func2();")
324 with captured_stdout() as stdout:
325 self._coverage(tracer, cmd)
326 stdout.seek(0)
327 stdout.readline()
328 coverage = {}
329 for line in stdout:
330 lines, cov, module = line.split()[:3]
331 coverage[module] = (int(lines), int(cov[:-1]))
Alexander Belopolskya847c812010-09-24 22:04:22 +0000332 # XXX This is needed to run regrtest.py as a script
333 modname = trace.fullmodname(sys.modules[modname].__file__)
Alexander Belopolskyff09ce22010-09-24 18:03:12 +0000334 self.assertIn(modname, coverage)
335 self.assertEqual(coverage[modname], (5, 100))
336
Georg Brandl283b1252010-08-02 12:48:46 +0000337
338def test_main():
339 run_unittest(__name__)
340
Alexander Belopolsky4d770172010-09-13 18:14:34 +0000341
342if __name__ == '__main__':
Georg Brandl283b1252010-08-02 12:48:46 +0000343 test_main()