test_collector.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Tests of coverage/collector.py and other collectors."""
  4. import os.path
  5. import coverage
  6. from tests.coveragetest import CoverageTest
  7. from tests.helpers import CheckUniqueFilenames
  8. class CollectorTest(CoverageTest):
  9. """Test specific aspects of the collection process."""
  10. def test_should_trace_cache(self):
  11. # The tracers should only invoke should_trace once for each file name.
  12. # Make some files that invoke each other.
  13. self.make_file("f1.py", """\
  14. def f1(x, f):
  15. return f(x)
  16. """)
  17. self.make_file("f2.py", """\
  18. import f1
  19. def func(x):
  20. return f1.f1(x, otherfunc)
  21. def otherfunc(x):
  22. return x*x
  23. for i in range(10):
  24. func(i)
  25. """)
  26. # Trace one file, but not the other. CheckUniqueFilenames will assert
  27. # that _should_trace hasn't been called twice for the same file.
  28. cov = coverage.Coverage(include=["f1.py"])
  29. should_trace_hook = CheckUniqueFilenames.hook(cov, '_should_trace')
  30. # Import the Python file, executing it.
  31. self.start_import_stop(cov, "f2")
  32. # Double-check that our files were checked.
  33. abs_files = set(os.path.abspath(f) for f in should_trace_hook.filenames)
  34. self.assertIn(os.path.abspath("f1.py"), abs_files)
  35. self.assertIn(os.path.abspath("f2.py"), abs_files)