#!/usr/bin/env python3 """运行测试并生成现代化中文测试报告 Dashboard 用法: python scripts/run_and_report.py # 默认运行全部测试 python scripts/run_and_report.py --tests tests/data_space/ # 只跑某个模块 python scripts/run_and_report.py --no-xdist # 串行运行(调试) python scripts/run_and_report.py --no-history # 不保存历史记录 """ import subprocess import sys import argparse from pathlib import Path ROOT = Path(__file__).parent.parent def run(cmd: list, description: str) -> int: print(f"\n{'━' * 60}") print(f" {description}") print(f"{'━' * 60}") result = subprocess.run(cmd, cwd=str(ROOT)) return result.returncode def main(): parser = argparse.ArgumentParser(description="运行测试并生成 Dashboard 报告") parser.add_argument( "--tests", nargs="+", default=["tests/"], help="测试目录或文件(默认 tests/),支持多个路径", ) parser.add_argument( "--no-xdist", action="store_true", help="禁用并行执行(用于调试)" ) parser.add_argument( "--output", default=str(ROOT / "reports" / "report_zh.html"), help="报告输出路径", ) parser.add_argument( "--no-history", action="store_true", help="不保存历史数据", ) args = parser.parse_args() # Step 1: Run tests pytest_cmd = [sys.executable, "-m", "pytest"] + args.tests if args.no_xdist: pytest_cmd += [ "-p", "no:xdist", "--override-ini=addopts=-v --strict-markers --tb=short", ] exit_code = run(pytest_cmd, "运行自动化测试") # Step 2: Generate report report_cmd = [ sys.executable, str(ROOT / "scripts" / "generate_report.py"), "--output", args.output, ] if args.no_history: report_cmd.append("--no-history") run(report_cmd, "生成测试报告 Dashboard") # Step 3: Summary print(f"\n{'━' * 60}") if exit_code == 0: print(" ALL TESTS PASSED") else: print(f" 测试完成,退出码: {exit_code}") print(f" 报告: {args.output}") print(f"{'━' * 60}\n") sys.exit(exit_code) if __name__ == "__main__": main()