This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathconftest.py
More file actions
151 lines (117 loc) · 3.8 KB
/
conftest.py
File metadata and controls
151 lines (117 loc) · 3.8 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Shared pytest fixtures and configuration for all tests."""
import os
import sys
import tempfile
from pathlib import Path
from typing import Generator, Dict, Any
import pytest
# Add project root to Python path
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
"""Create a temporary directory for test files."""
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)
@pytest.fixture
def temp_file(temp_dir: Path) -> Generator[Path, None, None]:
"""Create a temporary file for testing."""
temp_path = temp_dir / "test_file.txt"
temp_path.write_text("test content")
yield temp_path
@pytest.fixture
def mock_config() -> Dict[str, Any]:
"""Provide a mock configuration dictionary."""
return {
"debug": True,
"max_iterations": 100,
"timeout": 30,
"output_dir": "/tmp/test_output",
"model_name": "test_model",
}
@pytest.fixture
def sample_puzzle_data() -> Dict[str, Any]:
"""Provide sample puzzle data for testing."""
return {
"name": "test_puzzle",
"description": "A test puzzle for unit testing",
"input": [1, 2, 3, 4, 5],
"expected_output": 15,
"difficulty": "easy",
"tags": ["math", "sum"],
}
@pytest.fixture
def sample_code_snippet() -> str:
"""Provide a sample code snippet for testing."""
return """
def solve(input_data):
'''Solve the puzzle by summing all numbers.'''
return sum(input_data)
"""
@pytest.fixture
def mock_environment_variables(monkeypatch):
"""Mock environment variables for testing."""
test_env = {
"TEST_MODE": "true",
"LOG_LEVEL": "DEBUG",
"OUTPUT_FORMAT": "json",
}
for key, value in test_env.items():
monkeypatch.setenv(key, value)
return test_env
@pytest.fixture(scope="session")
def project_root() -> Path:
"""Return the project root directory."""
return PROJECT_ROOT
@pytest.fixture
def generators_path(project_root: Path) -> Path:
"""Return the path to the generators module."""
return project_root / "generators"
@pytest.fixture
def solvers_path(project_root: Path) -> Path:
"""Return the path to the solvers module."""
return project_root / "solvers"
@pytest.fixture
def clean_imports():
"""Clean up imports to ensure test isolation."""
modules_to_remove = []
for module in sys.modules:
if module.startswith(("generators", "solvers")):
modules_to_remove.append(module)
yield
for module in modules_to_remove:
if module in sys.modules:
del sys.modules[module]
@pytest.fixture(autouse=True)
def reset_random_seed():
"""Reset random seed for reproducible tests."""
import random
import numpy as np
random.seed(42)
np.random.seed(42)
yield
# Reset after test
random.seed()
np.random.seed()
def pytest_configure(config):
"""Configure pytest with custom settings."""
config.addinivalue_line(
"markers", "unit: Unit tests"
)
config.addinivalue_line(
"markers", "integration: Integration tests"
)
config.addinivalue_line(
"markers", "slow: Slow tests that should be run less frequently"
)
def pytest_collection_modifyitems(config, items):
"""Modify test collection to add markers based on test location."""
for item in items:
# Add markers based on test file location
if "unit" in str(item.fspath):
item.add_marker(pytest.mark.unit)
elif "integration" in str(item.fspath):
item.add_marker(pytest.mark.integration)
# Add slow marker to tests with "slow" in their name
if "slow" in item.nodeid.lower():
item.add_marker(pytest.mark.slow)