-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest
More file actions
executable file
·515 lines (420 loc) · 15.3 KB
/
test
File metadata and controls
executable file
·515 lines (420 loc) · 15.3 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python3
"""
AgentGate Test Runner
Parallel test execution with Allure reporting for professional test visualization.
Usage:
./test Run all tests in parallel (auto-opens report)
./test -k "pii" Pass-through to pytest
./test tests/test_agent.py Specific file
./test --no-parallel Sequential mode
./test --workers 4 Specific worker count
./test --no-open Don't auto-open Allure report
./test --report Generate and open Allure HTML report (no tests)
./test --serve Serve Allure report (live reload)
./test --help-test Show this help
"""
import os
import re
import shutil
import signal
import subprocess
import sys
import time
from atexit import register
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent
ALLURE_RESULTS = PROJECT_ROOT / "allure-results"
ALLURE_REPORT = PROJECT_ROOT / "allure-report"
ALLURE_HISTORY = PROJECT_ROOT / ".allure-history"
_ACTIVE_CHILD: subprocess.Popen[str] | None = None
_INTERRUPT_SIGNAL: int | None = None
def get_python_executable():
"""Return the Python executable to use for running pytest."""
venv_python = PROJECT_ROOT / ".venv" / "bin" / "python"
if venv_python.is_file():
return str(venv_python)
return sys.executable
def _signal_label(signum: int) -> str:
"""Return readable signal name for logs."""
try:
return signal.Signals(signum).name
except ValueError:
return str(signum)
def _terminate_process_tree(
process: subprocess.Popen[str],
*,
grace_seconds: float = 5.0,
) -> None:
"""Terminate the child process and all descendants."""
if process.poll() is not None:
return
if os.name == "posix":
try:
pgid = os.getpgid(process.pid)
except ProcessLookupError:
return
try:
os.killpg(pgid, signal.SIGTERM)
except ProcessLookupError:
return
except OSError:
process.terminate()
else:
process.terminate()
deadline = time.monotonic() + grace_seconds
while process.poll() is None and time.monotonic() < deadline:
time.sleep(0.1)
if process.poll() is not None:
return
if os.name == "posix":
try:
pgid = os.getpgid(process.pid)
os.killpg(pgid, signal.SIGKILL)
except ProcessLookupError:
return
except OSError:
process.kill()
else:
process.kill()
def _shutdown_handler(signum: int, _frame) -> None:
"""Forward shutdown signals to the active child process group."""
global _INTERRUPT_SIGNAL
_INTERRUPT_SIGNAL = signum
if _ACTIVE_CHILD is None:
return
print(
f"\n{YELLOW}Received {_signal_label(signum)}; terminating test workers...{RESET}",
file=sys.stderr,
)
_terminate_process_tree(_ACTIVE_CHILD)
def _install_shutdown_handlers() -> dict[int, object]:
"""Install shutdown handlers and return prior handlers."""
previous: dict[int, object] = {}
signals = [signal.SIGINT, signal.SIGTERM]
if hasattr(signal, "SIGHUP"):
signals.append(signal.SIGHUP)
for sig in signals:
previous[sig] = signal.getsignal(sig)
signal.signal(sig, _shutdown_handler)
return previous
def _restore_shutdown_handlers(previous: dict[int, object]) -> None:
"""Restore prior signal handlers after child completion."""
for sig, handler in previous.items():
signal.signal(sig, handler)
def _cleanup_active_child() -> None:
"""Best-effort reaping for abnormal parent exits."""
if _ACTIVE_CHILD is None:
return
_terminate_process_tree(_ACTIVE_CHILD, grace_seconds=1.0)
register(_cleanup_active_child)
# ANSI color codes
GREEN = "\033[32m"
RED = "\033[31m"
YELLOW = "\033[33m"
CYAN = "\033[36m"
BOLD = "\033[1m"
DIM = "\033[2m"
RESET = "\033[0m"
HELP_TEXT = f"""{BOLD}AgentGate Test Runner{RESET}
{BOLD}USAGE:{RESET}
./test Run all tests in parallel (auto-opens report)
./test [PYTEST_ARGS...] Pass arguments through to pytest
./test [OPTIONS] Runner-specific options
{BOLD}OPTIONS:{RESET}
--no-parallel Run tests sequentially (no xdist)
--workers N Use N parallel workers (default: auto)
--no-open Don't auto-open Allure report after tests
--no-clean Keep previous test results (accumulate)
--report Generate and open Allure HTML report (no tests)
--serve Serve Allure report with live reload
--clean Clear all Allure results, reports, and history
--help-test Show this help message
{BOLD}EXAMPLES:{RESET}
./test Run all tests, auto-open report when done
./test -k "pii" Run only tests matching "pii"
./test tests/test_agent.py Run a specific test file
./test --workers 4 Use exactly 4 workers
./test --no-parallel -x Run sequentially, stop on first failure
./test --no-open Run tests without opening report
./test --report View Allure report in browser (no tests)
{BOLD}ALLURE REPORTS:{RESET}
Test results are saved to allure-results/. After each test run, the Allure
report automatically opens in your browser showing pass/fail trends,
categories, and detailed failure traces.
History is preserved in .allure-history/ for trend graphs across runs.
"""
def parse_runner_args(argv):
"""Separate runner-specific flags from pytest arguments."""
runner_opts = {
"no_parallel": False,
"workers": None,
"no_open": False,
"no_clean": False,
"report": False,
"serve": False,
"clean": False,
"help_test": False,
}
pytest_args = []
i = 0
while i < len(argv):
arg = argv[i]
if arg == "--no-parallel":
runner_opts["no_parallel"] = True
elif arg == "--workers":
i += 1
if i < len(argv):
runner_opts["workers"] = int(argv[i])
else:
print(f"{RED}Error: --workers requires a number{RESET}", file=sys.stderr)
sys.exit(2)
elif arg == "--no-open":
runner_opts["no_open"] = True
elif arg == "--no-clean":
runner_opts["no_clean"] = True
elif arg == "--report":
runner_opts["report"] = True
elif arg == "--serve":
runner_opts["serve"] = True
elif arg == "--clean":
runner_opts["clean"] = True
elif arg == "--help-test":
runner_opts["help_test"] = True
else:
pytest_args.append(arg)
i += 1
return runner_opts, pytest_args
def parse_pytest_output(output: str) -> tuple[int, int, int, int, int]:
"""Parse pytest summary line to extract test counts.
Returns: (passed, failed, errors, skipped, warnings)
"""
# Match patterns like "1654 passed, 1 failed, 4454 warnings"
passed = failed = errors = skipped = warnings = 0
# Look for the summary line
match = re.search(r'(\d+) passed', output)
if match:
passed = int(match.group(1))
match = re.search(r'(\d+) failed', output)
if match:
failed = int(match.group(1))
match = re.search(r'(\d+) error', output)
if match:
errors = int(match.group(1))
match = re.search(r'(\d+) skipped', output)
if match:
skipped = int(match.group(1))
match = re.search(r'(\d+) warning', output)
if match:
warnings = int(match.group(1))
return passed, failed, errors, skipped, warnings
def print_summary(
passed,
failed,
errors,
skipped,
warnings,
duration,
workers,
result_code,
interrupt_signal,
):
"""Print a short post-run summary."""
interrupted = interrupt_signal is not None
is_pass = result_code == 0 and failed == 0 and errors == 0 and not interrupted
if interrupted:
status = f"{YELLOW}{BOLD}INTERRUPTED{RESET}"
else:
status = (
f"{GREEN}{BOLD}PASSED{RESET}"
if is_pass
else f"{RED}{BOLD}FAILED{RESET}"
)
parts = []
if passed:
parts.append(f"{GREEN}{passed} passed{RESET}")
if failed:
parts.append(f"{RED}{failed} failed{RESET}")
if errors:
parts.append(f"{RED}{errors} errors{RESET}")
if skipped:
parts.append(f"{YELLOW}{skipped} skipped{RESET}")
if warnings:
parts.append(f"{YELLOW}{warnings} warnings{RESET}")
if interrupted:
parts.append(
f"{YELLOW}interrupted ({_signal_label(interrupt_signal)}){RESET}"
)
worker_info = f" ({workers} workers)" if workers and workers != "sequential" else ""
print()
print(f"{BOLD}{'=' * 70}{RESET}")
print(f" {status} | {', '.join(parts) if parts else 'no tests'} | {duration:.1f}s{worker_info}")
print(f"{BOLD}{'=' * 70}{RESET}")
print()
def preserve_history():
"""Copy history from previous report to preserve trends."""
history_src = ALLURE_REPORT / "history"
if history_src.exists():
ALLURE_HISTORY.mkdir(exist_ok=True)
# Copy history files
for item in history_src.iterdir():
dest = ALLURE_HISTORY / item.name
if item.is_file():
shutil.copy2(item, dest)
def restore_history():
"""Restore history to results directory for trend preservation."""
if ALLURE_HISTORY.exists():
history_dest = ALLURE_RESULTS / "history"
history_dest.mkdir(exist_ok=True)
for item in ALLURE_HISTORY.iterdir():
if item.is_file():
shutil.copy2(item, history_dest / item.name)
def clean_results_before_run():
"""Clean results directory before running tests (preserves history)."""
# First, preserve existing history
preserve_history()
# Remove old results
if ALLURE_RESULTS.exists():
shutil.rmtree(ALLURE_RESULTS)
# Create fresh results directory
ALLURE_RESULTS.mkdir(exist_ok=True)
# Restore history for trends
restore_history()
def run_tests(runner_opts, pytest_args):
"""Execute pytest with Allure reporting."""
global _ACTIVE_CHILD, _INTERRUPT_SIGNAL
python = get_python_executable()
cmd = [python, "-m", "pytest"]
# Determine parallelism
workers = None
if not runner_opts["no_parallel"]:
if runner_opts["workers"]:
workers = runner_opts["workers"]
cmd.extend(["-n", str(workers)])
else:
workers = "auto"
cmd.extend(["-n", "auto"])
cmd.extend(["--dist", "loadfile"])
# Allure results directory
cmd.extend(["--alluredir", str(ALLURE_RESULTS)])
cmd.extend(pytest_args)
# Clean previous results unless --no-clean is specified
if not runner_opts["no_clean"]:
print(f"{DIM}Cleaning previous test results (preserving history)...{RESET}")
clean_results_before_run()
# Print what we're running
parallel_desc = "sequential" if runner_opts["no_parallel"] else f"-n {workers} --dist loadfile"
print(f"{CYAN}Running: {parallel_desc}{RESET}")
print(f"{DIM}{' '.join(cmd)}{RESET}")
print()
start = time.monotonic()
_INTERRUPT_SIGNAL = None
previous_handlers = _install_shutdown_handlers()
popen_kwargs: dict[str, object] = {
"cwd": PROJECT_ROOT,
"stdout": subprocess.PIPE,
"stderr": subprocess.STDOUT,
"text": True,
"bufsize": 1,
}
if os.name == "posix":
popen_kwargs["start_new_session"] = True
output_chunks: list[str] = []
result_code = 1
try:
process = subprocess.Popen(cmd, **popen_kwargs)
_ACTIVE_CHILD = process
assert process.stdout is not None
for line in process.stdout:
print(line, end="")
output_chunks.append(line)
result_code = process.wait()
finally:
_ACTIVE_CHILD = None
_restore_shutdown_handlers(previous_handlers)
duration = time.monotonic() - start
# Parse results from pytest output
output = "".join(output_chunks)
passed, failed, errors, skipped, warnings = parse_pytest_output(output)
print_summary(
passed, failed, errors, skipped, warnings,
duration,
workers if not runner_opts["no_parallel"] else None,
result_code,
_INTERRUPT_SIGNAL,
)
# Auto-open report unless --no-open is specified
if not runner_opts["no_open"]:
print(f"{CYAN}Generating and opening Allure report...{RESET}")
generate_report(open_browser=True)
else:
print(f"{DIM}Run ./test --report to view the Allure report{RESET}")
if _INTERRUPT_SIGNAL is not None:
return 128 + _INTERRUPT_SIGNAL
return result_code
def generate_report(open_browser=True):
"""Generate Allure HTML report and optionally open in browser."""
if not ALLURE_RESULTS.exists() or not list(ALLURE_RESULTS.glob("*")):
print(f"{RED}No test results found. Run ./test first.{RESET}")
return 1
# Preserve history before generating new report
preserve_history()
print(f"{DIM}Generating Allure report...{RESET}")
result = subprocess.run(
["npx", "allure-commandline", "generate", str(ALLURE_RESULTS),
"-o", str(ALLURE_REPORT), "--clean"],
cwd=PROJECT_ROOT,
capture_output=True,
text=True
)
if result.returncode != 0:
print(f"{RED}Failed to generate report. Make sure npx is installed.{RESET}")
if result.stderr:
print(result.stderr)
return 1
print(f"{GREEN}Report generated at {ALLURE_REPORT}{RESET}")
if open_browser:
print(f"{CYAN}Opening report in browser...{RESET}")
subprocess.Popen(
["npx", "allure-commandline", "open", str(ALLURE_REPORT)],
cwd=PROJECT_ROOT,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return 0
def serve_report():
"""Serve Allure report with live reload."""
if not ALLURE_RESULTS.exists() or not list(ALLURE_RESULTS.glob("*")):
print(f"{RED}No test results found. Run ./test first.{RESET}")
return 1
print(f"{CYAN}Serving Allure report (Ctrl+C to stop)...{RESET}")
subprocess.run(
["npx", "allure-commandline", "serve", str(ALLURE_RESULTS)],
cwd=PROJECT_ROOT
)
return 0
def clean_all():
"""Remove all Allure results, reports, and history."""
count = 0
if ALLURE_RESULTS.exists():
count += len(list(ALLURE_RESULTS.rglob("*")))
shutil.rmtree(ALLURE_RESULTS)
if ALLURE_REPORT.exists():
shutil.rmtree(ALLURE_REPORT)
if ALLURE_HISTORY.exists():
shutil.rmtree(ALLURE_HISTORY)
print(f"{GREEN}Cleared Allure results, reports, and history ({count} files).{RESET}")
def main():
runner_opts, pytest_args = parse_runner_args(sys.argv[1:])
if runner_opts["help_test"]:
print(HELP_TEXT)
return 0
if runner_opts["clean"]:
clean_all()
return 0
if runner_opts["report"]:
return generate_report(open_browser=True)
if runner_opts["serve"]:
return serve_report()
return run_tests(runner_opts, pytest_args)
if __name__ == "__main__":
sys.exit(main())