Georg Brandl | 283b125 | 2010-08-02 12:48:46 +0000 | [diff] [blame] | 1 | # Testing the trace module |
| 2 | |
| 3 | from test.support import run_unittest, TESTFN, rmtree, unlink, captured_stdout |
| 4 | import unittest |
| 5 | import trace |
| 6 | import os, sys |
| 7 | |
| 8 | class TestCoverage(unittest.TestCase): |
| 9 | def tearDown(self): |
| 10 | rmtree(TESTFN) |
| 11 | unlink(TESTFN) |
| 12 | |
| 13 | def _coverage(self, tracer): |
| 14 | tracer.run('from test import test_pprint; test_pprint.test_main()') |
| 15 | r = tracer.results() |
| 16 | r.write_results(show_missing=True, summary=True, coverdir=TESTFN) |
| 17 | |
| 18 | def test_coverage(self): |
| 19 | tracer = trace.Trace(trace=0, count=1) |
| 20 | with captured_stdout() as stdout: |
| 21 | self._coverage(tracer) |
| 22 | stdout = stdout.getvalue() |
| 23 | self.assertTrue("pprint.py" in stdout) |
| 24 | self.assertTrue("case.py" in stdout) # from unittest |
| 25 | files = os.listdir(TESTFN) |
| 26 | self.assertTrue("pprint.cover" in files) |
| 27 | self.assertTrue("unittest.case.cover" in files) |
| 28 | |
| 29 | def test_coverage_ignore(self): |
| 30 | # Ignore all files, nothing should be traced nor printed |
| 31 | libpath = os.path.normpath(os.path.dirname(os.__file__)) |
| 32 | # sys.prefix does not work when running from a checkout |
| 33 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix, libpath], |
| 34 | trace=0, count=1) |
| 35 | with captured_stdout() as stdout: |
| 36 | self._coverage(tracer) |
| 37 | self.assertEquals(stdout.getvalue(), "") |
| 38 | if os.path.exists(TESTFN): |
| 39 | files = os.listdir(TESTFN) |
| 40 | self.assertEquals(files, []) |
| 41 | |
| 42 | |
| 43 | def test_main(): |
| 44 | run_unittest(__name__) |
| 45 | |
| 46 | if __name__ == "__main__": |
| 47 | test_main() |