Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | from test.support import (run_unittest, TESTFN, rmtree, unlink, |
| 4 | captured_stdout) |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 5 | import unittest |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 6 | |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 7 | import trace |
| 8 | from trace import CoverageResults, Trace |
| 9 | |
| 10 | from test.tracedmodules import testmod |
| 11 | |
| 12 | |
| 13 | #------------------------------- Utilities -----------------------------------# |
| 14 | |
| 15 | def 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 | |
| 21 | def 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 | |
| 26 | def 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 | |
| 36 | def traced_func_linear(x, y): |
| 37 | a = x |
| 38 | b = y |
| 39 | c = a + b |
| 40 | return c |
| 41 | |
| 42 | def traced_func_loop(x, y): |
| 43 | c = x |
| 44 | for i in range(5): |
| 45 | c += y |
| 46 | return c |
| 47 | |
| 48 | def traced_func_importing(x, y): |
| 49 | return x + y + testmod.func(1) |
| 50 | |
| 51 | def traced_func_simple_caller(x): |
| 52 | c = traced_func_linear(x, x) |
| 53 | return c + x |
| 54 | |
| 55 | def traced_func_importing_caller(x): |
| 56 | k = traced_func_simple_caller(x) |
| 57 | k += traced_func_importing(k, x) |
| 58 | return k |
| 59 | |
| 60 | def traced_func_generator(num): |
| 61 | c = 5 # executed once |
| 62 | for i in range(num): |
| 63 | yield i + c |
| 64 | |
| 65 | def traced_func_calling_generator(): |
| 66 | k = 0 |
| 67 | for i in traced_func_generator(10): |
| 68 | k += i |
| 69 | |
| 70 | def traced_doubler(num): |
| 71 | return num * 2 |
| 72 | |
| 73 | def traced_caller_list_comprehension(): |
| 74 | k = 10 |
| 75 | mylist = [traced_doubler(i) for i in range(k)] |
| 76 | return mylist |
| 77 | |
| 78 | |
| 79 | class 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 | |
| 102 | class TestLineCounts(unittest.TestCase): |
| 103 | """White-box testing of line-counting, via runfunc""" |
| 104 | def setUp(self): |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 105 | self.addCleanup(sys.settrace, sys.gettrace()) |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 106 | self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
| 107 | self.my_py_filename = fix_ext_py(__file__) |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 108 | |
| 109 | def test_traced_func_linear(self): |
| 110 | result = self.tracer.runfunc(traced_func_linear, 2, 5) |
| 111 | self.assertEqual(result, 7) |
| 112 | |
| 113 | # all lines are executed once |
| 114 | expected = {} |
| 115 | firstlineno = get_firstlineno(traced_func_linear) |
| 116 | for i in range(1, 5): |
| 117 | expected[(self.my_py_filename, firstlineno + i)] = 1 |
| 118 | |
| 119 | self.assertEqual(self.tracer.results().counts, expected) |
| 120 | |
| 121 | def test_traced_func_loop(self): |
| 122 | self.tracer.runfunc(traced_func_loop, 2, 3) |
| 123 | |
| 124 | firstlineno = get_firstlineno(traced_func_loop) |
| 125 | expected = { |
| 126 | (self.my_py_filename, firstlineno + 1): 1, |
| 127 | (self.my_py_filename, firstlineno + 2): 6, |
| 128 | (self.my_py_filename, firstlineno + 3): 5, |
| 129 | (self.my_py_filename, firstlineno + 4): 1, |
| 130 | } |
| 131 | self.assertEqual(self.tracer.results().counts, expected) |
| 132 | |
| 133 | def test_traced_func_importing(self): |
| 134 | self.tracer.runfunc(traced_func_importing, 2, 5) |
| 135 | |
| 136 | firstlineno = get_firstlineno(traced_func_importing) |
| 137 | expected = { |
| 138 | (self.my_py_filename, firstlineno + 1): 1, |
| 139 | (fix_ext_py(testmod.__file__), 2): 1, |
| 140 | (fix_ext_py(testmod.__file__), 3): 1, |
| 141 | } |
| 142 | |
| 143 | self.assertEqual(self.tracer.results().counts, expected) |
| 144 | |
| 145 | def test_trace_func_generator(self): |
| 146 | self.tracer.runfunc(traced_func_calling_generator) |
| 147 | |
| 148 | firstlineno_calling = get_firstlineno(traced_func_calling_generator) |
| 149 | firstlineno_gen = get_firstlineno(traced_func_generator) |
| 150 | expected = { |
| 151 | (self.my_py_filename, firstlineno_calling + 1): 1, |
| 152 | (self.my_py_filename, firstlineno_calling + 2): 11, |
| 153 | (self.my_py_filename, firstlineno_calling + 3): 10, |
| 154 | (self.my_py_filename, firstlineno_gen + 1): 1, |
| 155 | (self.my_py_filename, firstlineno_gen + 2): 11, |
| 156 | (self.my_py_filename, firstlineno_gen + 3): 10, |
| 157 | } |
| 158 | self.assertEqual(self.tracer.results().counts, expected) |
| 159 | |
| 160 | def test_trace_list_comprehension(self): |
| 161 | self.tracer.runfunc(traced_caller_list_comprehension) |
| 162 | |
| 163 | firstlineno_calling = get_firstlineno(traced_caller_list_comprehension) |
| 164 | firstlineno_called = get_firstlineno(traced_doubler) |
| 165 | expected = { |
| 166 | (self.my_py_filename, firstlineno_calling + 1): 1, |
| 167 | # List compehentions work differently in 3.x, so the count |
| 168 | # below changed compared to 2.x. |
| 169 | (self.my_py_filename, firstlineno_calling + 2): 12, |
| 170 | (self.my_py_filename, firstlineno_calling + 3): 1, |
| 171 | (self.my_py_filename, firstlineno_called + 1): 10, |
| 172 | } |
| 173 | self.assertEqual(self.tracer.results().counts, expected) |
| 174 | |
| 175 | |
| 176 | def test_linear_methods(self): |
| 177 | # XXX todo: later add 'static_method_linear' and 'class_method_linear' |
| 178 | # here, once issue1764286 is resolved |
| 179 | # |
| 180 | for methname in ['inst_method_linear',]: |
| 181 | tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
| 182 | traced_obj = TracedClass(25) |
| 183 | method = getattr(traced_obj, methname) |
| 184 | tracer.runfunc(method, 20) |
| 185 | |
| 186 | firstlineno = get_firstlineno(method) |
| 187 | expected = { |
| 188 | (self.my_py_filename, firstlineno + 1): 1, |
| 189 | } |
| 190 | self.assertEqual(tracer.results().counts, expected) |
| 191 | |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 192 | class TestRunExecCounts(unittest.TestCase): |
| 193 | """A simple sanity test of line-counting, via runctx (exec)""" |
| 194 | def setUp(self): |
| 195 | self.my_py_filename = fix_ext_py(__file__) |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 196 | self.addCleanup(sys.settrace, sys.gettrace()) |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 197 | |
| 198 | def test_exec_counts(self): |
| 199 | self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) |
| 200 | code = r'''traced_func_loop(2, 5)''' |
| 201 | code = compile(code, __file__, 'exec') |
| 202 | self.tracer.runctx(code, globals(), vars()) |
| 203 | |
| 204 | firstlineno = get_firstlineno(traced_func_loop) |
| 205 | expected = { |
| 206 | (self.my_py_filename, firstlineno + 1): 1, |
| 207 | (self.my_py_filename, firstlineno + 2): 6, |
| 208 | (self.my_py_filename, firstlineno + 3): 5, |
| 209 | (self.my_py_filename, firstlineno + 4): 1, |
| 210 | } |
| 211 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 212 | # When used through 'run', some other spurious counts are produced, like |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 213 | # the settrace of threading, which we ignore, just making sure that the |
| 214 | # counts fo traced_func_loop were right. |
| 215 | # |
| 216 | for k in expected.keys(): |
| 217 | self.assertEqual(self.tracer.results().counts[k], expected[k]) |
| 218 | |
| 219 | |
| 220 | class TestFuncs(unittest.TestCase): |
| 221 | """White-box testing of funcs tracing""" |
| 222 | def setUp(self): |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 223 | self.addCleanup(sys.settrace, sys.gettrace()) |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 224 | self.tracer = Trace(count=0, trace=0, countfuncs=1) |
| 225 | self.filemod = my_file_and_modname() |
| 226 | |
| 227 | def test_simple_caller(self): |
| 228 | self.tracer.runfunc(traced_func_simple_caller, 1) |
| 229 | |
| 230 | expected = { |
| 231 | self.filemod + ('traced_func_simple_caller',): 1, |
| 232 | self.filemod + ('traced_func_linear',): 1, |
| 233 | } |
| 234 | self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 235 | |
| 236 | def test_loop_caller_importing(self): |
| 237 | self.tracer.runfunc(traced_func_importing_caller, 1) |
| 238 | |
| 239 | expected = { |
| 240 | self.filemod + ('traced_func_simple_caller',): 1, |
| 241 | self.filemod + ('traced_func_linear',): 1, |
| 242 | self.filemod + ('traced_func_importing_caller',): 1, |
| 243 | self.filemod + ('traced_func_importing',): 1, |
| 244 | (fix_ext_py(testmod.__file__), 'testmod', 'func'): 1, |
| 245 | } |
| 246 | self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 247 | |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 248 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 249 | 'pre-existing trace function throws off measurements') |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 250 | def test_inst_method_calling(self): |
| 251 | obj = TracedClass(20) |
| 252 | self.tracer.runfunc(obj.inst_method_calling, 1) |
| 253 | |
| 254 | expected = { |
| 255 | self.filemod + ('TracedClass.inst_method_calling',): 1, |
| 256 | self.filemod + ('TracedClass.inst_method_linear',): 1, |
| 257 | self.filemod + ('traced_func_linear',): 1, |
| 258 | } |
| 259 | self.assertEqual(self.tracer.results().calledfuncs, expected) |
| 260 | |
| 261 | |
| 262 | class TestCallers(unittest.TestCase): |
| 263 | """White-box testing of callers tracing""" |
| 264 | def setUp(self): |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 265 | self.addCleanup(sys.settrace, sys.gettrace()) |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 266 | self.tracer = Trace(count=0, trace=0, countcallers=1) |
| 267 | self.filemod = my_file_and_modname() |
| 268 | |
Brett Cannon | 7a54073 | 2011-02-22 03:04:06 +0000 | [diff] [blame] | 269 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 270 | 'pre-existing trace function throws off measurements') |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 271 | def test_loop_caller_importing(self): |
| 272 | self.tracer.runfunc(traced_func_importing_caller, 1) |
| 273 | |
| 274 | expected = { |
| 275 | ((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'), |
| 276 | (self.filemod + ('traced_func_importing_caller',))): 1, |
| 277 | ((self.filemod + ('traced_func_simple_caller',)), |
| 278 | (self.filemod + ('traced_func_linear',))): 1, |
| 279 | ((self.filemod + ('traced_func_importing_caller',)), |
| 280 | (self.filemod + ('traced_func_simple_caller',))): 1, |
| 281 | ((self.filemod + ('traced_func_importing_caller',)), |
| 282 | (self.filemod + ('traced_func_importing',))): 1, |
| 283 | ((self.filemod + ('traced_func_importing',)), |
| 284 | (fix_ext_py(testmod.__file__), 'testmod', 'func')): 1, |
| 285 | } |
| 286 | self.assertEqual(self.tracer.results().callers, expected) |
| 287 | |
| 288 | |
| 289 | # Created separately for issue #3821 |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 290 | class TestCoverage(unittest.TestCase): |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 291 | def setUp(self): |
| 292 | self.addCleanup(sys.settrace, sys.gettrace()) |
| 293 | |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 294 | def tearDown(self): |
| 295 | rmtree(TESTFN) |
| 296 | unlink(TESTFN) |
| 297 | |
Alexander Belopolsky | ff09ce2 | 2010-09-24 18:03:12 +0000 | [diff] [blame] | 298 | def _coverage(self, tracer, |
| 299 | cmd='from test import test_pprint; test_pprint.test_main()'): |
| 300 | tracer.run(cmd) |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 301 | r = tracer.results() |
| 302 | r.write_results(show_missing=True, summary=True, coverdir=TESTFN) |
| 303 | |
| 304 | def test_coverage(self): |
| 305 | tracer = trace.Trace(trace=0, count=1) |
| 306 | with captured_stdout() as stdout: |
| 307 | self._coverage(tracer) |
| 308 | stdout = stdout.getvalue() |
| 309 | self.assertTrue("pprint.py" in stdout) |
| 310 | self.assertTrue("case.py" in stdout) # from unittest |
| 311 | files = os.listdir(TESTFN) |
| 312 | self.assertTrue("pprint.cover" in files) |
| 313 | self.assertTrue("unittest.case.cover" in files) |
| 314 | |
| 315 | def test_coverage_ignore(self): |
| 316 | # Ignore all files, nothing should be traced nor printed |
| 317 | libpath = os.path.normpath(os.path.dirname(os.__file__)) |
| 318 | # sys.prefix does not work when running from a checkout |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 319 | tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix, |
| 320 | libpath], trace=0, count=1) |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 321 | with captured_stdout() as stdout: |
| 322 | self._coverage(tracer) |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 323 | if os.path.exists(TESTFN): |
| 324 | files = os.listdir(TESTFN) |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 325 | self.assertEqual(files, ['_importlib.cover']) # Ignore __import__ |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 326 | |
Alexander Belopolsky | ff09ce2 | 2010-09-24 18:03:12 +0000 | [diff] [blame] | 327 | def test_issue9936(self): |
| 328 | tracer = trace.Trace(trace=0, count=1) |
| 329 | modname = 'test.tracedmodules.testmod' |
| 330 | # Ensure that the module is executed in import |
| 331 | if modname in sys.modules: |
| 332 | del sys.modules[modname] |
| 333 | cmd = ("import test.tracedmodules.testmod as t;" |
| 334 | "t.func(0); t.func2();") |
| 335 | with captured_stdout() as stdout: |
| 336 | self._coverage(tracer, cmd) |
| 337 | stdout.seek(0) |
| 338 | stdout.readline() |
| 339 | coverage = {} |
| 340 | for line in stdout: |
| 341 | lines, cov, module = line.split()[:3] |
| 342 | coverage[module] = (int(lines), int(cov[:-1])) |
Alexander Belopolsky | a847c81 | 2010-09-24 22:04:22 +0000 | [diff] [blame] | 343 | # XXX This is needed to run regrtest.py as a script |
Alexander Belopolsky | 1f75f5d | 2010-11-26 18:51:39 +0000 | [diff] [blame] | 344 | modname = trace._fullmodname(sys.modules[modname].__file__) |
Alexander Belopolsky | ff09ce2 | 2010-09-24 18:03:12 +0000 | [diff] [blame] | 345 | self.assertIn(modname, coverage) |
| 346 | self.assertEqual(coverage[modname], (5, 100)) |
| 347 | |
Alexander Belopolsky | 6672ea9 | 2010-11-08 18:32:40 +0000 | [diff] [blame] | 348 | ### Tests that don't mess with sys.settrace and can be traced |
| 349 | ### themselves TODO: Skip tests that do mess with sys.settrace when |
| 350 | ### regrtest is invoked with -T option. |
| 351 | class Test_Ignore(unittest.TestCase): |
| 352 | def test_ignored(self): |
Alexander Belopolsky | 18c3373 | 2010-11-08 23:10:20 +0000 | [diff] [blame] | 353 | jn = os.path.join |
Alexander Belopolsky | 1f75f5d | 2010-11-26 18:51:39 +0000 | [diff] [blame] | 354 | ignore = trace._Ignore(['x', 'y.z'], [jn('foo', 'bar')]) |
Alexander Belopolsky | 6672ea9 | 2010-11-08 18:32:40 +0000 | [diff] [blame] | 355 | self.assertTrue(ignore.names('x.py', 'x')) |
| 356 | self.assertFalse(ignore.names('xy.py', 'xy')) |
| 357 | self.assertFalse(ignore.names('y.py', 'y')) |
Alexander Belopolsky | 18c3373 | 2010-11-08 23:10:20 +0000 | [diff] [blame] | 358 | self.assertTrue(ignore.names(jn('foo', 'bar', 'baz.py'), 'baz')) |
| 359 | self.assertFalse(ignore.names(jn('bar', 'z.py'), 'z')) |
Alexander Belopolsky | 6672ea9 | 2010-11-08 18:32:40 +0000 | [diff] [blame] | 360 | # Matched before. |
Alexander Belopolsky | 18c3373 | 2010-11-08 23:10:20 +0000 | [diff] [blame] | 361 | self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz')) |
Alexander Belopolsky | 6672ea9 | 2010-11-08 18:32:40 +0000 | [diff] [blame] | 362 | |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 363 | |
| 364 | def test_main(): |
| 365 | run_unittest(__name__) |
| 366 | |
Alexander Belopolsky | 4d77017 | 2010-09-13 18:14:34 +0000 | [diff] [blame] | 367 | |
| 368 | if __name__ == '__main__': |
Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 369 | test_main() |