-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
276 lines (228 loc) · 7.76 KB
/
Copy pathrun_tests.py
File metadata and controls
276 lines (228 loc) · 7.76 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
Test runner for preserve - runs all tests and provides coverage report.
Usage:
python run_tests.py # Run all tests
python run_tests.py -v # Run with verbose output
python run_tests.py --coverage # Run with coverage report
python run_tests.py --generate # Generate test data first
python run_tests.py test_manifest # Run specific test module
"""
import sys
import os
import unittest
import argparse
from pathlib import Path
import subprocess
import json
# Add current directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def setup_test_environment():
"""Set up the test environment."""
# Ensure test-runs directory exists
test_runs_dir = Path("test-runs")
test_runs_dir.mkdir(exist_ok=True)
# Set environment variables if needed
os.environ['PRESERVE_TEST_MODE'] = '1'
print("Test environment setup complete.")
def generate_test_data():
"""Generate test data using the generator script."""
print("\nGenerating test data...")
result = subprocess.run(
[sys.executable, "tests/generate_test_data.py"],
capture_output=True,
text=True
)
if result.returncode == 0:
print("Test data generated successfully.")
# Parse the output to get the test directory
for line in result.stdout.split('\n'):
if line.startswith("Base directory:"):
test_dir = line.split(":", 1)[1].strip()
return test_dir
else:
print(f"Error generating test data: {result.stderr}")
return None
def run_tests(test_pattern="test_*.py", verbosity=2, test_dir="tests"):
"""Run the unit tests."""
# Discover and run tests
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# Get test files but exclude one-offs directory
test_path = Path(test_dir)
for test_file in test_path.glob(test_pattern):
# Skip files in subdirectories like one-offs
if test_file.parent == test_path:
module_name = f"{test_dir}.{test_file.stem}"
try:
module = __import__(module_name, fromlist=[''])
suite.addTests(loader.loadTestsFromModule(module))
except ImportError as e:
print(f"Warning: Could not import {module_name}: {e}")
# Run tests
runner = unittest.TextTestRunner(verbosity=verbosity)
result = runner.run(suite)
return result.wasSuccessful()
def run_with_coverage(test_pattern="test_*.py", test_dir="tests"):
"""Run tests with coverage reporting."""
try:
import coverage
except ImportError:
print("Coverage package not installed. Install with: pip install coverage")
return False
# Initialize coverage
cov = coverage.Coverage(
source=["preserve", "preservelib", "dazzle_filekit"],
omit=[
"*/tests/*",
"*/test_*.py",
"*/__pycache__/*",
"*/setup.py"
]
)
# Start coverage
cov.start()
# Run tests
success = run_tests(test_pattern, verbosity=2, test_dir=test_dir)
# Stop coverage
cov.stop()
cov.save()
# Generate report
print("\n" + "=" * 70)
print("COVERAGE REPORT")
print("=" * 70)
cov.report()
# Generate HTML report
html_dir = Path("htmlcov")
cov.html_report(directory=str(html_dir))
print(f"\nDetailed HTML coverage report generated in: {html_dir.absolute()}")
return success
def run_specific_test(test_name, verbosity=2):
"""Run a specific test module or test case."""
loader = unittest.TestLoader()
# Try to load the specific test
try:
if '.' in test_name:
# Full test path like test_manifest.TestManifest.test_numbering
suite = loader.loadTestsFromName(f"tests.{test_name}")
else:
# Module name like test_manifest
suite = loader.loadTestsFromModule(
__import__(f"tests.{test_name}", fromlist=[''])
)
except (ImportError, AttributeError) as e:
print(f"Error loading test '{test_name}': {e}")
return False
# Run the test
runner = unittest.TextTestRunner(verbosity=verbosity)
result = runner.run(suite)
return result.wasSuccessful()
def list_tests():
"""List all available test modules."""
test_dir = Path("tests")
test_files = sorted(test_dir.glob("test_*.py"))
print("\nAvailable test modules:")
print("-" * 40)
for test_file in test_files:
module_name = test_file.stem
print(f" {module_name}")
# Try to get test class names
try:
module = __import__(f"tests.{module_name}", fromlist=[''])
for name in dir(module):
if name.startswith('Test'):
cls = getattr(module, name)
if isinstance(cls, type) and issubclass(cls, unittest.TestCase):
print(f" - {name}")
except ImportError:
pass
print("-" * 40)
print("\nRun specific test with: python run_tests.py <test_name>")
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Run preserve test suite",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python run_tests.py # Run all tests
python run_tests.py -v # Run with verbose output
python run_tests.py --coverage # Run with coverage report
python run_tests.py --generate # Generate test data first
python run_tests.py test_manifest # Run specific test module
python run_tests.py --list # List available tests
"""
)
parser.add_argument(
"test",
nargs="?",
help="Specific test module or test case to run"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Verbose test output"
)
parser.add_argument(
"--coverage",
action="store_true",
help="Run tests with coverage analysis"
)
parser.add_argument(
"--generate",
action="store_true",
help="Generate test data before running tests"
)
parser.add_argument(
"--list",
action="store_true",
help="List available test modules"
)
parser.add_argument(
"--test-dir",
default="tests",
help="Directory containing test files (default: tests)"
)
args = parser.parse_args()
# Setup test environment
setup_test_environment()
# List tests if requested
if args.list:
list_tests()
return 0
# Generate test data if requested
if args.generate:
test_data_dir = generate_test_data()
if test_data_dir:
print(f"Test data available in: {test_data_dir}")
else:
print("Failed to generate test data")
return 1
# Determine verbosity
verbosity = 2 if args.verbose else 1
# Run tests
print("\n" + "=" * 70)
print("PRESERVE TEST SUITE")
print("=" * 70)
if args.test:
# Run specific test
print(f"\nRunning specific test: {args.test}")
success = run_specific_test(args.test, verbosity)
elif args.coverage:
# Run with coverage
print("\nRunning tests with coverage analysis...")
success = run_with_coverage(test_dir=args.test_dir)
else:
# Run all tests
print("\nRunning all tests...")
success = run_tests(verbosity=verbosity, test_dir=args.test_dir)
# Report results
print("\n" + "=" * 70)
if success:
print("ALL TESTS PASSED [OK]")
else:
print("SOME TESTS FAILED [FAIL]")
print("=" * 70)
return 0 if success else 1
if __name__ == "__main__":
sys.exit(main())