Coverage for tests / conftest.py: 100.000%
45 statements
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-29 20:34 +0000
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-29 20:34 +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"""
5Pytest auto configuration.
7This module is run automatically by pytest, to define and enable fixtures.
8"""
10from __future__ import annotations 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
12import os 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
13import sys 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
14import warnings 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
16from collections.abc import Iterable 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
18import pytest 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
20from coverage.files import set_relative_directory 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
21from coverage.patch import create_pth_files 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
23from tests import testenv 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
26# Pytest will rewrite assertions in test modules, but not elsewhere.
27# This tells pytest to also rewrite assertions in these files:
28pytest.register_assert_rewrite("tests.coveragetest") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
29pytest.register_assert_rewrite("tests.helpers") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
31# Pytest can take additional options:
32# $set_env.py: PYTEST_ADDOPTS - Extra arguments to pytest.
34pytest_plugins = [ 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
35 "tests.select_plugin",
36]
39@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
40def set_warnings() -> None: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
41 """Configure warnings to show while running tests."""
42 warnings.simplefilter("default") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
43 warnings.simplefilter("once", DeprecationWarning) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
45 # Warnings to suppress:
46 # How come these warnings are successfully suppressed here, but not in pyproject.toml??
48 # Note: when writing the regex for the message, it's matched with re.match,
49 # so it has to match the beginning of the message. Add ".*" to make it
50 # match something in the middle of the message.
52 # Don't warn about unclosed SQLite connections.
53 # We don't close ":memory:" databases because we don't have a way to connect
54 # to them more than once if we close them. In real coverage.py uses, there
55 # are only a couple of them, but our test suite makes many and we get warned
56 # about them all.
57 # Python3.13 added this warning, but the behavior has been the same all along,
58 # without any reported problems, so just quiet the warning.
59 # https://github.com/python/cpython/issues/105539
60 warnings.filterwarnings("ignore", r"unclosed database", category=ResourceWarning) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
62 # We have a test that has a return in a finally: test_bug_1891.
63 warnings.filterwarnings("ignore", "'return' in a 'finally' block", category=SyntaxWarning) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
65 # For when our own tests can't use sysmon though it was requested.
66 warnings.filterwarnings("ignore", r".*no-sysmon") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
67 if testenv.REQUESTED_CORE != "ctrace": 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
68 warnings.filterwarnings("ignore", r".*no-ctracer") 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
71@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
72def reset_sys_path() -> Iterable[None]: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
73 """Clean up sys.path changes around every test."""
74 sys_path = list(sys.path) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
75 yield 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
76 sys.path[:] = sys_path 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
79@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
80def reset_environment() -> Iterable[None]: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
81 """Make sure a test setting an envvar doesn't leak into another test."""
82 old_environ = os.environ.copy() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
83 yield 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
84 os.environ.clear() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
85 os.environ.update(old_environ) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
88@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
89def reset_filesdotpy_globals() -> None: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
90 """coverage/files.py has some unfortunate globals. Reset them every test."""
91 set_relative_directory() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
94@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
95def force_local_pyc_files() -> None: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
96 """Ensure that .pyc files are written next to source files."""
97 # For some tests, we need .pyc files written in the current directory,
98 # so override any local setting.
99 sys.pycache_prefix = None 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
102# Give this an underscored name so pylint won't complain when we use the fixture.
103@pytest.fixture(name="_create_pth_file") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
104def create_pth_file_fixture() -> Iterable[None]: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
105 """Create and clean up a .pth file for tests that need it for subprocesses."""
106 pth_files = create_pth_files() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
107 try: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
108 yield 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
109 finally:
110 for p in pth_files: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP
111 p.unlink() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP