Coverage for tests / conftest.py: 100.000%

37 statements  

« 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 

3 

4""" 

5Pytest auto configuration. 

6 

7This module is run automatically by pytest, to define and enable fixtures. 

8""" 

9 

10from __future__ import annotations 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

11 

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

15 

16from collections.abc import Iterable 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

17 

18import pytest 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

19 

20from coverage.files import set_relative_directory 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

21 

22from tests import testenv 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

23 

24 

25# Pytest will rewrite assertions in test modules, but not elsewhere. 

26# This tells pytest to also rewrite assertions in these files: 

27pytest.register_assert_rewrite("tests.coveragetest") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

28pytest.register_assert_rewrite("tests.helpers") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

29 

30# Pytest can take additional options: 

31# $set_env.py: PYTEST_ADDOPTS - Extra arguments to pytest. 

32 

33pytest_plugins = [ 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

34 "tests.select_plugin", 

35] 

36 

37 

38@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

39def set_warnings() -> None: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

40 """Configure warnings to show while running tests.""" 

41 warnings.simplefilter("default") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

42 warnings.simplefilter("once", DeprecationWarning) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

43 

44 # Warnings to suppress: 

45 # How come these warnings are successfully suppressed here, but not in pyproject.toml?? 

46 

47 # Note: when writing the regex for the message, it's matched with re.match, 

48 # so it has to match the beginning of the message. Add ".*" to make it 

49 # match something in the middle of the message. 

50 

51 # Don't warn about unclosed SQLite connections. 

52 # We don't close ":memory:" databases because we don't have a way to connect 

53 # to them more than once if we close them. In real coverage.py uses, there 

54 # are only a couple of them, but our test suite makes many and we get warned 

55 # about them all. 

56 # Python3.13 added this warning, but the behavior has been the same all along, 

57 # without any reported problems, so just quiet the warning. 

58 # https://github.com/python/cpython/issues/105539 

59 warnings.filterwarnings("ignore", r"unclosed database", category=ResourceWarning) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

60 

61 # We have a test that has a return in a finally: test_bug_1891. 

62 warnings.filterwarnings("ignore", "'return' in a 'finally' block", category=SyntaxWarning) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

63 

64 # For when our own tests can't use sysmon though it was requested. 

65 warnings.filterwarnings("ignore", r".*no-sysmon") 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

66 if testenv.REQUESTED_CORE != "ctrace": 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

67 warnings.filterwarnings("ignore", r".*no-ctracer") 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP

68 

69 

70@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

71def reset_sys_path() -> Iterable[None]: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

72 """Clean up sys.path changes around every test.""" 

73 sys_path = list(sys.path) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

74 yield 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

75 sys.path[:] = sys_path 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

76 

77 

78@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

79def reset_environment() -> Iterable[None]: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

80 """Make sure a test setting an envvar doesn't leak into another test.""" 

81 old_environ = os.environ.copy() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

82 yield 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

83 os.environ.clear() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

84 os.environ.update(old_environ) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

85 

86 

87@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

88def reset_filesdotpy_globals() -> None: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

89 """coverage/files.py has some unfortunate globals. Reset them every test.""" 

90 set_relative_directory() 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

91 

92 

93@pytest.fixture(autouse=True) 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

94def force_local_pyc_files() -> None: 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP

95 """Ensure that .pyc files are written next to source files.""" 

96 # For some tests, we need .pyc files written in the current directory, 

97 # so override any local setting. 

98 sys.pycache_prefix = None 1QaRbScTdUeVfWgXhYiZj0k1l2m3n4o5pq6rs7tu8vw9xy!zA#BC$DE%FG'HI(JK)LMNOP