TrafficWheel/test_configs.py

124 lines
3.7 KiB
Python

import os
import subprocess
import yaml
import time
# 配置路径
CONFIG_DIR = "/user/czzhangheng/code/TrafficWheel/config"
RUN_SCRIPT = "/user/czzhangheng/code/TrafficWheel/run.py"
RESULTS_FILE = "/user/czzhangheng/code/TrafficWheel/test_results.txt"
# 记录测试结果的字典
results = {
"passed": [],
"failed": [],
"error": []
}
# 遍历所有yaml文件
def find_all_yaml_files(directory):
yaml_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".yaml") and not file.startswith("BJTaxi"):
yaml_files.append(os.path.join(root, file))
return yaml_files
# 测试单个yaml文件
def test_yaml_file(yaml_path):
print(f"\n=== Testing {yaml_path} ===")
# 检查文件是否存在
if not os.path.exists(yaml_path):
print(f"File not found: {yaml_path}")
return "error", f"File not found: {yaml_path}"
# 运行测试命令
command = ["python", RUN_SCRIPT, "--config", yaml_path]
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=600 # 10分钟超时
)
# 分析结果
if result.returncode == 0:
if "Test passed" in result.stdout:
print(f"✓ PASSED: {yaml_path}")
return "passed", result.stdout.strip()
else:
print(f"✗ FAILED: {yaml_path}")
return "failed", result.stdout.strip() + "\n" + result.stderr.strip()
else:
print(f"✗ ERROR: {yaml_path}")
return "error", result.stdout.strip() + "\n" + result.stderr.strip()
except subprocess.TimeoutExpired:
print(f"✗ TIMEOUT: {yaml_path}")
return "error", "Timeout after 10 minutes"
except Exception as e:
print(f"✗ EXCEPTION: {yaml_path}")
return "error", str(e)
# 生成测试报告
def generate_report(results):
total = len(results["passed"]) + len(results["failed"]) + len(results["error"])
report = f"""# 测试报告
## 测试概述
- 测试时间: {time.strftime('%Y-%m-%d %H:%M:%S')}
- 总测试文件数: {total}
- 通过: {len(results['passed'])}
- 失败: {len(results['failed'])}
- 错误: {len(results['error'])}
## 通过的配置文件
"""
for file_path, output in results["passed"]:
report += f"- ✅ {file_path}\n"
report += "\n## 失败的配置文件\n"
for file_path, output in results["failed"]:
report += f"- ❌ {file_path}\n"
report += "\n## 出错的配置文件\n"
for file_path, output in results["error"]:
report += f"- ⚠️ {file_path}\n"
report += "\n## 详细输出\n"
for status, files in results.items():
report += f"\n### {status.upper()}\n\n"
for file_path, output in files:
report += f"#### {file_path}\n\n```\n{output}\n```\n\n"
return report
# 主函数
def main():
# 找到所有符合条件的yaml文件
yaml_files = find_all_yaml_files(CONFIG_DIR)
print(f"Found {len(yaml_files)} yaml files to test")
# 测试每个文件
for yaml_file in yaml_files:
status, output = test_yaml_file(yaml_file)
results[status].append((yaml_file, output))
# 生成并保存报告
report = generate_report(results)
with open(RESULTS_FILE, "w") as f:
f.write(report)
print(f"\n=== Test Results ===")
print(f"Total: {len(yaml_files)}")
print(f"Passed: {len(results['passed'])}")
print(f"Failed: {len(results['failed'])}")
print(f"Error: {len(results['error'])}")
print(f"Report saved to: {RESULTS_FILE}")
if __name__ == "__main__":
main()