8157f10768
- 重构为三角色协作:人+Arch AI+执行AI - 新增 Excel 寄存器表格解析工具,自动生成测试代码 - 新增串口日志分析工具,自动生成测试报告 - 完善项目文档:AGENTS.md、README.md - 创建自动化测试架构设计文档 - 添加示例测试任务 P01-001
120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
串口日志分析器:分析测试输出,验证结果
|
|
"""
|
|
|
|
import re
|
|
import json
|
|
import argparse
|
|
from collections import defaultdict
|
|
|
|
|
|
class LogAnalyzer:
|
|
def __init__(self, log_file):
|
|
self.log_file = log_file
|
|
self.results = {
|
|
'total': 0,
|
|
'passed': 0,
|
|
'failed': 0,
|
|
'tests': []
|
|
}
|
|
|
|
def analyze(self):
|
|
"""分析日志文件"""
|
|
with open(self.log_file, 'r', encoding='utf-8') as f:
|
|
lines = f.readlines()
|
|
|
|
current_test = None
|
|
for line in lines:
|
|
line = line.strip()
|
|
|
|
# 匹配 [TEST] 开始
|
|
test_start = re.match(r'\[TEST\]\s*(.*?):\s*开始', line)
|
|
if test_start:
|
|
current_test = {
|
|
'name': test_start.group(1),
|
|
'status': 'running',
|
|
'reg_tests': [],
|
|
'fails': []
|
|
}
|
|
self.results['total'] += 1
|
|
continue
|
|
|
|
# 匹配 [PASS]
|
|
pass_match = re.match(r'\[PASS\]\s*(.*?):\s*(.*)', line)
|
|
if pass_match and current_test:
|
|
reg_name = pass_match.group(1)
|
|
detail = pass_match.group(2)
|
|
current_test['reg_tests'].append({
|
|
'name': reg_name,
|
|
'status': 'PASS',
|
|
'detail': detail
|
|
})
|
|
continue
|
|
|
|
# 匹配 [FAIL]
|
|
fail_match = re.match(r'\[FAIL\]\s*(.*?):\s*(.*)', line)
|
|
if fail_match and current_test:
|
|
reg_name = fail_match.group(1)
|
|
detail = fail_match.group(2)
|
|
current_test['reg_tests'].append({
|
|
'name': reg_name,
|
|
'status': 'FAIL',
|
|
'detail': detail
|
|
})
|
|
current_test['fails'].append(f"{reg_name}: {detail}")
|
|
continue
|
|
|
|
# 匹配 [TEST] 结束
|
|
test_end = re.match(r'\[TEST\]\s*(.*?):\s*结束', line)
|
|
if test_end and current_test:
|
|
if current_test['fails']:
|
|
current_test['status'] = 'FAIL'
|
|
self.results['failed'] += 1
|
|
else:
|
|
current_test['status'] = 'PASS'
|
|
self.results['passed'] += 1
|
|
self.results['tests'].append(current_test)
|
|
current_test = None
|
|
|
|
return self.results
|
|
|
|
def generate_report(self, output_file):
|
|
"""生成测试报告"""
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
f.write("=" * 60 + "\n")
|
|
f.write("自动化测试报告\n")
|
|
f.write("=" * 60 + "\n\n")
|
|
f.write(f"总测试数: {self.results['total']}\n")
|
|
f.write(f"通过: {self.results['passed']}\n")
|
|
f.write(f"失败: {self.results['failed']}\n\n")
|
|
f.write("-" * 60 + "\n")
|
|
f.write("详细结果:\n\n")
|
|
|
|
for test in self.results['tests']:
|
|
f.write(f"测试: {test['name']}\n")
|
|
f.write(f"状态: {'✅ PASS' if test['status'] == 'PASS' else '❌ FAIL'}\n")
|
|
if test['fails']:
|
|
f.write("失败项:\n")
|
|
for fail in test['fails']:
|
|
f.write(f" - {fail}\n")
|
|
f.write("\n")
|
|
|
|
print(f"报告已生成: {output_file}")
|
|
print(f"统计: {self.results['passed']}/{self.results['total']} 通过")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='串口日志分析器')
|
|
parser.add_argument('log_file', help='日志文件路径')
|
|
parser.add_argument('-o', '--output', help='输出报告文件', default='test_report.txt')
|
|
args = parser.parse_args()
|
|
|
|
analyzer = LogAnalyzer(args.log_file)
|
|
analyzer.analyze()
|
|
analyzer.generate_report(args.output)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|