Coverage for tests / test_collector.py: 100.000%
15 statements
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
1# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
4"""Tests of coverage/collector.py and other collectors."""
6from __future__ import annotations 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
8import os.path 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
10import coverage 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
12from tests.coveragetest import CoverageTest 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
13from tests.helpers import CheckUniqueFilenames 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
16class CollectorTest(CoverageTest): 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
17 """Test specific aspects of the collection process."""
19 def test_should_trace_cache(self) -> None: 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
20 # The tracers should only invoke should_trace once for each file name.
22 # Make some files that invoke each other.
23 self.make_file( 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
24 "f1.py",
25 """\
26 def f1(x, f):
27 return f(x)
28 """,
29 )
31 self.make_file( 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
32 "f2.py",
33 """\
34 import f1
36 def func(x):
37 return f1.f1(x, otherfunc)
39 def otherfunc(x):
40 return x*x
42 for i in range(10):
43 func(i)
44 """,
45 )
47 # Trace one file, but not the other. CheckUniqueFilenames will assert
48 # that _should_trace hasn't been called twice for the same file.
49 cov = coverage.Coverage(include=["f1.py"]) 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
50 should_trace_hook = CheckUniqueFilenames.hook(cov, "_should_trace") 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
52 # Import the Python file, executing it.
53 self.start_import_stop(cov, "f2") 1abcdefghijklmnopqrstuvwxyzABCDEF5GH6IJ7KL8MN9OP!QR#ST$UV%WX'YZ(01)234
55 # Double-check that our files were checked.
56 abs_files = {os.path.abspath(f) for f in should_trace_hook.filenames} 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234
57 assert os.path.abspath("f1.py") in abs_files 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234
58 assert os.path.abspath("f2.py") in abs_files 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234