-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·251 lines (211 loc) · 6.34 KB
/
Copy pathrun_tests.py
File metadata and controls
executable file
·251 lines (211 loc) · 6.34 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
#!/usr/bin/env python3
"""
Test runner script for OCPP Proxy application.
This script provides various options for running the test suite:
- Run all tests
- Run specific test categories (unit, integration, e2e)
- Run with coverage reporting
- Run with different verbosity levels
"""
import sys
import os
import subprocess
import argparse
from pathlib import Path
def run_command(cmd, description):
"""Run a command and handle errors."""
print(f"\n{description}")
print("=" * len(description))
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=False)
if result.returncode != 0:
print(f"❌ {description} failed with exit code {result.returncode}")
return False
else:
print(f"✅ {description} completed successfully")
return True
def main():
"""Main test runner function."""
parser = argparse.ArgumentParser(description="Run OCPP Proxy test suite")
# Test type options
parser.add_argument(
"--unit",
action="store_true",
help="Run only unit tests"
)
parser.add_argument(
"--integration",
action="store_true",
help="Run only integration tests"
)
parser.add_argument(
"--e2e",
action="store_true",
help="Run only end-to-end tests"
)
parser.add_argument(
"--slow",
action="store_true",
help="Include slow tests"
)
# Coverage options
parser.add_argument(
"--no-coverage",
action="store_true",
help="Disable coverage reporting"
)
parser.add_argument(
"--coverage-html",
action="store_true",
help="Generate HTML coverage report"
)
# Output options
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Verbose output"
)
parser.add_argument(
"--quiet", "-q",
action="store_true",
help="Quiet output"
)
# Specific test options
parser.add_argument(
"--file",
type=str,
help="Run specific test file"
)
parser.add_argument(
"--function",
type=str,
help="Run specific test function"
)
# Other options
parser.add_argument(
"--install-deps",
action="store_true",
help="Install test dependencies before running tests"
)
parser.add_argument(
"--parallel",
action="store_true",
help="Run tests in parallel"
)
parser.add_argument(
"--stop-on-failure",
action="store_true",
help="Stop on first failure"
)
args = parser.parse_args()
# Change to project root directory
project_root = Path(__file__).parent
os.chdir(project_root)
# Install dependencies if requested
if args.install_deps:
if not run_command([
sys.executable, "-m", "pip", "install", "-r", "requirements.txt"
], "Installing test dependencies"):
return 1
# Build pytest command
cmd = [sys.executable, "-m", "pytest"]
# Add test selection markers
markers = []
if args.unit:
markers.append("unit")
if args.integration:
markers.append("integration")
if args.e2e:
markers.append("e2e")
if markers:
cmd.extend(["-m", " or ".join(markers)])
# Add slow tests if requested
if not args.slow:
if markers:
cmd.extend(["-m", f"({' or '.join(markers)}) and not slow"])
else:
cmd.extend(["-m", "not slow"])
# Add verbosity options
if args.verbose:
cmd.append("-v")
elif args.quiet:
cmd.append("-q")
# Add coverage options
if not args.no_coverage:
cmd.extend([
"--cov=src/ocpp_proxy",
"--cov-report=term-missing"
])
if args.coverage_html:
cmd.extend(["--cov-report=html:htmlcov"])
# Add parallel execution
if args.parallel:
try:
import pytest_xdist
cmd.extend(["-n", "auto"])
except ImportError:
print("⚠️ pytest-xdist not installed. Running tests sequentially.")
# Add stop on failure
if args.stop_on_failure:
cmd.append("-x")
# Add specific file or function
if args.file:
if args.function:
cmd.append(f"{args.file}::{args.function}")
else:
cmd.append(args.file)
elif args.function:
cmd.extend(["-k", args.function])
# Add test directory if no specific file
if not args.file:
cmd.append("tests/")
# Run the tests
success = run_command(cmd, "Running test suite")
if success:
print("\n🎉 All tests passed!")
if not args.no_coverage and not args.coverage_html:
print("\n📊 Coverage report generated in terminal above")
if args.coverage_html:
print("📊 HTML coverage report generated in htmlcov/index.html")
return 0
else:
print("\n💥 Tests failed!")
return 1
def run_specific_test_suites():
"""Run predefined test suites."""
print("OCPP Proxy Test Suite Runner")
print("=" * 40)
# Quick test suite (unit tests only)
print("\n1. Quick Test Suite (Unit Tests)")
quick_success = run_command([
sys.executable, "-m", "pytest",
"-m", "unit",
"--cov=src/ocpp_proxy",
"--cov-report=term-missing",
"tests/"
], "Quick test suite")
if not quick_success:
return 1
# Full test suite (all tests)
print("\n2. Full Test Suite (All Tests)")
full_success = run_command([
sys.executable, "-m", "pytest",
"--cov=src/ocpp_proxy",
"--cov-report=term-missing",
"--cov-report=html:htmlcov",
"tests/"
], "Full test suite")
if full_success:
print("\n🎉 All test suites passed!")
print("📊 HTML coverage report: htmlcov/index.html")
return 0
else:
print("\n💥 Some tests failed!")
return 1
if __name__ == "__main__":
if len(sys.argv) == 1:
# No arguments provided, run predefined suites
sys.exit(run_specific_test_suites())
else:
# Arguments provided, use argument parser
sys.exit(main())