-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathconftest.py
More file actions
85 lines (63 loc) · 2.36 KB
/
conftest.py
File metadata and controls
85 lines (63 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import ast
import sys # noqa
def has_test_functions(filepath):
"""
Parse a Python file and check whether it contains functions starting with 'test_'.
Also checks for test methods in classes.
"""
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
tree = ast.parse(content, filename=filepath)
for node in ast.walk(tree):
# Check for standalone test functions
if isinstance(node, ast.FunctionDef) and node.name.startswith('test_'):
return True
# Check for test methods in classes
if isinstance(node, ast.ClassDef):
for item in node.body:
if isinstance(item, ast.FunctionDef) and item.name.startswith('test_'):
return True
return False
except Exception as e:
# Log the error if you want to debug
print(f"Warning: Could not parse {filepath}: {e}")
return False
def pytest_ignore_collect(collection_path, config):
"""
Tell pytest which files to ignore
"""
s_path = str(collection_path)
if 'job_status' in s_path:
print('****found status: {}, {} ***'.format(s_path,format(collection_path.suffix)))
# Ignore legacy folders
if 'api_core' in s_path:
return True
if 'classification' in s_path:
return True
# if 'md_tests' in s_path:
# return True
# Let pytest figure out what to do with non-py files, including folders
if collection_path.suffix != '.py':
return False
# Ignore cache files
if '__pycache__' in s_path:
return True
# Don't ignore Python files that have test functions
if has_test_functions(collection_path):
print('Found test functions in {}'.format(s_path))
return False
# Ignore other .py files
return True
def pytest_collection_modifyitems(config, items):
"""
Log items discovered
"""
print(f"\nCustom collection found {len(items)} test items:")
for item in items:
print(f" {item.nodeid}")
def test_environment_debug():
print(f"PYTHONPATH from os.environ: {os.environ.get('PYTHONPATH', 'NOT SET')}")
print(f"sys.path: {sys.path}")
print(f"Current working directory: {os.getcwd()}")