-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_tests.py
More file actions
129 lines (102 loc) · 4 KB
/
run_tests.py
File metadata and controls
129 lines (102 loc) · 4 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
#!/usr/bin/env python3
"""
Test runner script for THEMAP project.
This script provides convenient commands to run different types of tests
with appropriate configurations.
Usage:
python run_tests.py [test_type] [options]
Test types:
all - Run all tests (default)
unit - Run only unit tests
integration - Run only integration tests
distance - Run only distance module tests
fast - Run tests excluding slow ones
coverage - Run tests with coverage report
Examples:
python run_tests.py # Run all tests
python run_tests.py unit # Run unit tests only
python run_tests.py fast # Skip slow tests
python run_tests.py coverage # Run with coverage
python run_tests.py distance -v # Run distance tests verbosely
"""
import argparse
import subprocess
import sys
from pathlib import Path
def run_command(cmd, description):
"""Run a command and handle errors."""
print(f"\n🚀 {description}")
print(f"Command: {' '.join(cmd)}")
print("-" * 60)
try:
result = subprocess.run(cmd, check=True, capture_output=False)
print(f"✅ {description} completed successfully")
return result.returncode
except subprocess.CalledProcessError as e:
print(f"❌ {description} failed with exit code {e.returncode}")
return e.returncode
except KeyboardInterrupt:
print(f"\n⚠️ {description} interrupted by user")
return 130
def main():
parser = argparse.ArgumentParser(
description="Run tests for THEMAP project",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"test_type",
nargs="?",
default="all",
choices=["all", "unit", "integration", "distance", "fast", "coverage"],
help="Type of tests to run (default: all)",
)
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
parser.add_argument("-x", "--stop-on-first-failure", action="store_true", help="Stop on first failure")
parser.add_argument("-k", "--keyword", help="Run tests matching given keyword expression")
parser.add_argument("--parallel", action="store_true", help="Run tests in parallel using pytest-xdist")
args = parser.parse_args()
# Base pytest command
cmd = ["python", "-m", "pytest"]
# Add test type specific options
if args.test_type == "unit":
cmd.append("tests/unit/")
description = "Running unit tests"
elif args.test_type == "integration":
cmd.append("tests/integration/")
description = "Running integration tests"
elif args.test_type == "distance":
cmd.extend(["tests/unit/distance/"])
description = "Running distance module tests"
elif args.test_type == "fast":
cmd.extend(["-m", "not slow"])
description = "Running fast tests (excluding slow tests)"
elif args.test_type == "coverage":
cmd.extend(["--cov=themap", "--cov-report=html", "--cov-report=term-missing"])
description = "Running tests with coverage analysis"
else: # all
description = "Running all tests"
# Add optional flags
if args.verbose:
cmd.append("-v")
if args.stop_on_first_failure:
cmd.append("-x")
if args.keyword:
cmd.extend(["-k", args.keyword])
if args.parallel:
cmd.extend(["-n", "auto"])
# Check if we're in the right directory
if not Path("pyproject.toml").exists():
print("❌ Error: pyproject.toml not found. Please run from project root.")
return 1
# Check if pytest is available
try:
subprocess.run(["python", "-m", "pytest", "--version"], check=True, capture_output=True)
except subprocess.CalledProcessError:
print("❌ Error: pytest not installed. Install with: pip install pytest")
return 1
# Run the tests
return run_command(cmd, description)
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)