Repository navigation
Expand file tree
/
Copy pathrun_all_tests.py
More file actions
269 lines (235 loc) · 8.93 KB
/
Copy pathrun_all_tests.py
File metadata and controls
269 lines (235 loc) · 8.93 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
#!/usr/bin/env python3
"""
Run camera_recorder tests on Windows or Linux from the same entry point.
Usage:
python run_all_tests.py # unit tests only (default, no cameras)
python run_all_tests.py --unit # same as default
python run_all_tests.py --smoke # real Face-On/DTL MediaPipe smoke
python run_all_tests.py --hardware # camera / FPS / recording scripts
python run_all_tests.py --all # unit + hardware
python run_all_tests.py --list # show what would run
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
import unittest
project_root = os.path.dirname(os.path.abspath(__file__))
tests_dir = os.path.join(project_root, 'tests')
sys.path.insert(0, os.path.join(project_root, 'src'))
sys.path.insert(0, tests_dir)
from test_utils import describe_platform_setup, fix_console_encoding, print_platform_banner
# Unittest modules that do not need real cameras
UNIT_TEST_MODULES = [
'test_platform_config',
'test_flask_gui',
'test_preview_while_recording',
'test_gui_stability',
'test_dual_camera_soak',
'test_mock_video_analysis',
'test_sway_calculator',
'test_swing_comparison',
'test_recording_management',
'test_swing_detector',
'test_archive',
'test_practice_features',
'test_local_db',
'test_setup_wizard',
'test_real_swing_smoke',
]
# Standalone scripts that open real cameras / write video
HARDWARE_TEST_SCRIPTS = [
'test_cameras.py',
'test_60fps.py',
'test_240fps_no_drops.py',
'test_frame_drops.py',
'test_golf_swing_settings.py',
'test_mediapipe_resolutions.py',
'test_videowriter_fps.py',
'verify_dual_recording.py',
]
def get_python_executable():
"""Prefer project .venv Python when present."""
if sys.platform == 'win32':
venv_python = os.path.join(project_root, '.venv', 'Scripts', 'python.exe')
else:
venv_python = os.path.join(project_root, '.venv', 'bin', 'python')
if os.path.exists(venv_python):
return venv_python
return sys.executable
def run_unittest_tests(python_exe, modules):
"""Run unittest modules one-by-one; return (passed, failed, details)."""
print("=" * 70)
print("Running unittest suites (no cameras required)...")
print("=" * 70)
details = {}
for test_name in modules:
print(f"\n--- tests.{test_name} ---")
try:
result = subprocess.run(
[python_exe, '-m', 'unittest', f'tests.{test_name}', '-v'],
cwd=project_root,
timeout=300,
)
ok = result.returncode == 0
except subprocess.TimeoutExpired:
print(f" [TIMEOUT] {test_name} exceeded 5 minute timeout")
ok = False
except Exception as exc:
print(f" [ERROR] Failed to run {test_name}: {exc}")
ok = False
details[test_name] = ok
passed = sum(1 for v in details.values() if v)
failed = len(details) - passed
return passed, failed, details
def run_hardware_tests(python_exe, scripts):
"""Run standalone hardware scripts; return (passed, failed, skipped, details)."""
print("\n" + "=" * 70)
print("Running hardware / camera scripts...")
print("=" * 70)
print("These need real USB cameras. Skip with: python run_all_tests.py --unit")
details = {}
for test_file in scripts:
test_path = os.path.join(tests_dir, test_file)
if not os.path.exists(test_path):
details[test_file] = None
continue
print(f"\n{'=' * 70}")
print(f"Running: {test_file}")
print('=' * 70)
try:
result = subprocess.run(
[python_exe, test_path],
cwd=project_root,
timeout=300,
)
details[test_file] = result.returncode == 0
except subprocess.TimeoutExpired:
print(f" [TIMEOUT] {test_file} exceeded 5 minute timeout")
details[test_file] = False
except Exception as exc:
print(f" [ERROR] Failed to run {test_file}: {exc}")
details[test_file] = False
passed = sum(1 for v in details.values() if v is True)
failed = sum(1 for v in details.values() if v is False)
skipped = sum(1 for v in details.values() if v is None)
return passed, failed, skipped, details
def main(argv=None):
fix_console_encoding()
parser = argparse.ArgumentParser(
description='Cross-platform test runner for camera_recorder'
)
group = parser.add_mutually_exclusive_group()
group.add_argument('--unit', action='store_true', default=False,
help='Run unittest suites only (default)')
group.add_argument('--hardware', action='store_true',
help='Run camera/FPS hardware scripts only')
group.add_argument('--all', action='store_true',
help='Run unit + hardware tests')
parser.add_argument('--smoke', action='store_true',
help='Run real Face-On/DTL MediaPipe smoke (opt-in)')
parser.add_argument('--list', action='store_true',
help='List tests and platform setup, then exit')
args = parser.parse_args(argv)
# Default mode is unit-only when no flag is given
run_unit = args.unit or args.all or not (args.hardware or args.all or args.smoke)
run_hw = args.hardware or args.all
run_smoke = args.smoke
if args.hardware and not args.all:
run_unit = False
if not args.unit and not args.hardware and not args.all and not args.smoke:
run_unit = True
run_hw = False
if args.smoke and not args.unit and not args.all:
run_unit = False
python_exe = get_python_executable()
print_platform_banner("CAMERA RECORDER - TEST SUITE")
print(f"Using Python: {python_exe}")
modes = []
if run_unit:
modes.append('unit')
if run_smoke:
modes.append('smoke')
if run_hw:
modes.append('hardware')
print(f"Mode: {'+'.join(modes) or 'unit'}")
if args.list:
print("\nUnit test modules:")
for name in UNIT_TEST_MODULES:
print(f" tests.{name}")
print("\nReal-swing smoke:")
print(" scripts/smoke_real_swings.py (python run_all_tests.py --smoke)")
print("\nHardware scripts:")
for name in HARDWARE_TEST_SCRIPTS:
print(f" tests/{name}")
return 0
# Dependency check (opencv/numpy required for almost everything)
try:
result = subprocess.run(
[python_exe, '-c', 'import cv2; import numpy'],
cwd=project_root,
capture_output=True,
timeout=15,
)
if result.returncode != 0:
print("\nERROR: Required dependencies not found!")
print("Install with: pip install -r requirements.txt")
return 1
except Exception as exc:
print(f"Warning: Could not verify dependencies: {exc}")
unit_details = {}
hw_details = {}
smoke_ok = None
unit_failed = 0
hw_failed = 0
if run_unit:
_, unit_failed, unit_details = run_unittest_tests(python_exe, UNIT_TEST_MODULES)
if run_smoke:
print("\n" + "=" * 70)
print("Running real Face-On / DTL MediaPipe smoke...")
print("=" * 70)
smoke_env = os.environ.copy()
smoke_env['SWINGLAB_REAL_SWING_SMOKE'] = '1'
try:
smoke_result = subprocess.run(
[python_exe, os.path.join(project_root, 'scripts', 'smoke_real_swings.py')],
cwd=project_root,
timeout=300,
env=smoke_env,
)
smoke_ok = smoke_result.returncode == 0
except subprocess.TimeoutExpired:
print(" [TIMEOUT] real-swing smoke exceeded 5 minute timeout")
smoke_ok = False
except Exception as exc:
print(f" [ERROR] real-swing smoke failed: {exc}")
smoke_ok = False
if run_hw:
_, hw_failed, _, hw_details = run_hardware_tests(python_exe, HARDWARE_TEST_SCRIPTS)
print("\n" + "=" * 70)
print("TEST SUMMARY")
print("=" * 70)
if unit_details:
print("Unit tests:")
for name, ok in unit_details.items():
print(f" {name}: {'PASSED' if ok else 'FAILED'}")
if hw_details:
print("Hardware tests:")
for name, result in hw_details.items():
if result is None:
status = "SKIPPED (not found)"
elif result:
status = "PASSED"
else:
status = "FAILED"
print(f" {name}: {status}")
if smoke_ok is not None:
print(f"Real-swing smoke: {'PASSED' if smoke_ok else 'FAILED'}")
overall_ok = unit_failed == 0 and hw_failed == 0 and smoke_ok is not False
print("\n" + "=" * 70)
print("ALL TESTS PASSED" if overall_ok else "SOME TESTS FAILED")
print("=" * 70)
return 0 if overall_ok else 1
if __name__ == "__main__":
sys.exit(main())