- 16 个业务模块,856 个测试用例,40 个 E2E 测试 - 单一 React 19 前端 + PlatformSwitcher 二合一切换 - Flask 后端支持 scenario/dcloud_module/dcloud_e2e/dcloud_all 四种任务类型 - E2E 测试面板:API 全链路(8条) + UI 点击(3条) + 分步测试(3个) - SSE 实时日志流 + 飞书告警 + 定时调度 - 完整还原原 ToB 平台 TestPanel E2E 模块
430 lines
14 KiB
Python
430 lines
14 KiB
Python
"""
|
||
测试执行指标收集系统
|
||
|
||
功能:
|
||
- 收集测试执行时间
|
||
- 收集测试成功率
|
||
- 收集失败原因
|
||
- 生成指标报告
|
||
"""
|
||
|
||
import json
|
||
import time
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
from typing import Any, Dict, List, Optional
|
||
from dataclasses import dataclass, field, asdict
|
||
from enum import Enum
|
||
|
||
|
||
class TestStatus(Enum):
|
||
"""测试状态枚举"""
|
||
PASSED = "passed"
|
||
FAILED = "failed"
|
||
SKIPPED = "skipped"
|
||
ERROR = "error"
|
||
|
||
|
||
@dataclass
|
||
class TestResult:
|
||
"""单个测试结果"""
|
||
test_id: str # 测试ID(文件路径::测试类::测试方法)
|
||
test_name: str # 测试名称
|
||
status: TestStatus
|
||
duration: float # 执行时间(秒)
|
||
start_time: str # 开始时间
|
||
end_time: str # 结束时间
|
||
error_message: Optional[str] = None # 错误信息
|
||
error_traceback: Optional[str] = None # 错误堆栈
|
||
markers: List[str] = field(default_factory=list) # pytest markers
|
||
file_path: str = "" # 测试文件路径
|
||
class_name: str = "" # 测试类名
|
||
retries: int = 0 # 重试次数
|
||
|
||
|
||
@dataclass
|
||
class TestSuiteMetrics:
|
||
"""测试套件指标"""
|
||
suite_name: str
|
||
total_tests: int = 0
|
||
passed: int = 0
|
||
failed: int = 0
|
||
skipped: int = 0
|
||
error: int = 0
|
||
total_duration: float = 0.0
|
||
start_time: str = ""
|
||
end_time: str = ""
|
||
results: List[TestResult] = field(default_factory=list)
|
||
|
||
@property
|
||
def success_rate(self) -> float:
|
||
"""成功率"""
|
||
if self.total_tests == 0:
|
||
return 0.0
|
||
return (self.passed / self.total_tests) * 100
|
||
|
||
@property
|
||
def avg_duration(self) -> float:
|
||
"""平均执行时间"""
|
||
if self.total_tests == 0:
|
||
return 0.0
|
||
return self.total_duration / self.total_tests
|
||
|
||
|
||
class MetricsCollector:
|
||
"""指标收集器"""
|
||
|
||
def __init__(self, output_dir: str = "test-results/metrics"):
|
||
self.output_dir = Path(output_dir)
|
||
self.output_dir.mkdir(parents=True, exist_ok=True)
|
||
|
||
self.current_suite: Optional[TestSuiteMetrics] = None
|
||
self.current_test: Optional[TestResult] = None
|
||
self.test_start_time: float = 0.0
|
||
|
||
def start_suite(self, suite_name: str):
|
||
"""开始测试套件"""
|
||
self.current_suite = TestSuiteMetrics(
|
||
suite_name=suite_name,
|
||
start_time=datetime.now().isoformat()
|
||
)
|
||
|
||
def end_suite(self) -> Optional[TestSuiteMetrics]:
|
||
"""结束测试套件"""
|
||
if not self.current_suite:
|
||
return None
|
||
|
||
self.current_suite.end_time = datetime.now().isoformat()
|
||
|
||
# 保存指标
|
||
self._save_metrics()
|
||
|
||
suite = self.current_suite
|
||
self.current_suite = None
|
||
return suite
|
||
|
||
def start_test(self, test_id: str, test_name: str, file_path: str = "", class_name: str = "", markers: List[str] = None):
|
||
"""开始测试"""
|
||
self.test_start_time = time.time()
|
||
self.current_test = TestResult(
|
||
test_id=test_id,
|
||
test_name=test_name,
|
||
status=TestStatus.PASSED,
|
||
duration=0.0,
|
||
start_time=datetime.now().isoformat(),
|
||
end_time="",
|
||
file_path=file_path,
|
||
class_name=class_name,
|
||
markers=markers or []
|
||
)
|
||
|
||
def end_test(self, status: TestStatus, error_message: str = None, error_traceback: str = None):
|
||
"""结束测试"""
|
||
if not self.current_test or not self.current_suite:
|
||
return
|
||
|
||
end_time = time.time()
|
||
duration = end_time - self.test_start_time
|
||
|
||
self.current_test.status = status
|
||
self.current_test.duration = duration
|
||
self.current_test.end_time = datetime.now().isoformat()
|
||
self.current_test.error_message = error_message
|
||
self.current_test.error_traceback = error_traceback
|
||
|
||
# 更新套件指标
|
||
self.current_suite.results.append(self.current_test)
|
||
self.current_suite.total_tests += 1
|
||
self.current_suite.total_duration += duration
|
||
|
||
if status == TestStatus.PASSED:
|
||
self.current_suite.passed += 1
|
||
elif status == TestStatus.FAILED:
|
||
self.current_suite.failed += 1
|
||
elif status == TestStatus.SKIPPED:
|
||
self.current_suite.skipped += 1
|
||
elif status == TestStatus.ERROR:
|
||
self.current_suite.error += 1
|
||
|
||
self.current_test = None
|
||
|
||
def record_retry(self):
|
||
"""记录重试"""
|
||
if self.current_test:
|
||
self.current_test.retries += 1
|
||
|
||
def _save_metrics(self):
|
||
"""保存指标到文件"""
|
||
if not self.current_suite:
|
||
return
|
||
|
||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||
filename = f"metrics_{timestamp}.json"
|
||
filepath = self.output_dir / filename
|
||
|
||
# 转换为字典
|
||
metrics_dict = asdict(self.current_suite)
|
||
|
||
# 转换枚举为字符串
|
||
metrics_dict["results"] = [
|
||
{**r, "status": r["status"].value if isinstance(r["status"], TestStatus) else r["status"]}
|
||
for r in metrics_dict["results"]
|
||
]
|
||
|
||
with open(filepath, 'w', encoding='utf-8') as f:
|
||
json.dump(metrics_dict, f, indent=2, ensure_ascii=False)
|
||
|
||
def get_summary(self) -> Dict[str, Any]:
|
||
"""获取指标摘要"""
|
||
if not self.current_suite:
|
||
return {}
|
||
|
||
return {
|
||
"suite_name": self.current_suite.suite_name,
|
||
"total_tests": self.current_suite.total_tests,
|
||
"passed": self.current_suite.passed,
|
||
"failed": self.current_suite.failed,
|
||
"skipped": self.current_suite.skipped,
|
||
"error": self.current_suite.error,
|
||
"success_rate": f"{self.current_suite.success_rate:.2f}%",
|
||
"total_duration": f"{self.current_suite.total_duration:.2f}s",
|
||
"avg_duration": f"{self.current_suite.avg_duration:.2f}s"
|
||
}
|
||
|
||
|
||
class MetricsReporter:
|
||
"""指标报告生成器"""
|
||
|
||
def __init__(self, metrics_dir: str = "test-results/metrics"):
|
||
self.metrics_dir = Path(metrics_dir)
|
||
|
||
def generate_report(self, metrics: TestSuiteMetrics) -> str:
|
||
"""生成文本报告"""
|
||
report = []
|
||
report.append("=" * 80)
|
||
report.append(f"测试执行报告 - {metrics.suite_name}")
|
||
report.append("=" * 80)
|
||
report.append("")
|
||
|
||
# 基本统计
|
||
report.append("## 基本统计")
|
||
report.append(f"- 总测试数: {metrics.total_tests}")
|
||
report.append(f"- 通过: {metrics.passed} ✅")
|
||
report.append(f"- 失败: {metrics.failed} ❌")
|
||
report.append(f"- 跳过: {metrics.skipped} ⏭️")
|
||
report.append(f"- 错误: {metrics.error} ⚠️")
|
||
report.append(f"- 成功率: {metrics.success_rate:.2f}%")
|
||
report.append("")
|
||
|
||
# 时间统计
|
||
report.append("## 时间统计")
|
||
report.append(f"- 开始时间: {metrics.start_time}")
|
||
report.append(f"- 结束时间: {metrics.end_time}")
|
||
report.append(f"- 总耗时: {metrics.total_duration:.2f}s")
|
||
report.append(f"- 平均耗时: {metrics.avg_duration:.2f}s")
|
||
report.append("")
|
||
|
||
# 失败测试详情
|
||
if metrics.failed > 0 or metrics.error > 0:
|
||
report.append("## 失败测试详情")
|
||
for result in metrics.results:
|
||
if result.status in [TestStatus.FAILED, TestStatus.ERROR]:
|
||
report.append(f"\n### {result.test_name}")
|
||
report.append(f"- 状态: {result.status.value}")
|
||
report.append(f"- 耗时: {result.duration:.2f}s")
|
||
if result.error_message:
|
||
report.append(f"- 错误: {result.error_message}")
|
||
if result.error_traceback:
|
||
report.append(f"- 堆栈:\n```\n{result.error_traceback}\n```")
|
||
report.append("")
|
||
|
||
# 性能警告
|
||
report.append("## 性能警告")
|
||
slow_tests = [r for r in metrics.results if r.duration > 10.0]
|
||
if slow_tests:
|
||
report.append(f"\n发现 {len(slow_tests)} 个慢测试(>10s):")
|
||
for test in sorted(slow_tests, key=lambda x: x.duration, reverse=True)[:10]:
|
||
report.append(f"- {test.test_name}: {test.duration:.2f}s")
|
||
else:
|
||
report.append("\n✅ 无慢测试")
|
||
|
||
report.append("")
|
||
report.append("=" * 80)
|
||
|
||
return "\n".join(report)
|
||
|
||
def save_report(self, metrics: TestSuiteMetrics, output_file: str = None):
|
||
"""保存报告到文件"""
|
||
if output_file is None:
|
||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||
output_file = f"test-report_{timestamp}.txt"
|
||
|
||
report = self.generate_report(metrics)
|
||
output_path = Path(output_file)
|
||
output_path.write_text(report, encoding='utf-8')
|
||
|
||
return str(output_path)
|
||
|
||
def generate_html_report(self, metrics: TestSuiteMetrics) -> str:
|
||
"""生成HTML报告"""
|
||
html = f"""
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>测试执行报告 - {metrics.suite_name}</title>
|
||
<style>
|
||
body {{
|
||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
||
max-width: 1200px;
|
||
margin: 0 auto;
|
||
padding: 20px;
|
||
background: #f5f5f5;
|
||
}}
|
||
.header {{
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 30px;
|
||
border-radius: 10px;
|
||
margin-bottom: 20px;
|
||
}}
|
||
.stats-grid {{
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||
gap: 15px;
|
||
margin-bottom: 20px;
|
||
}}
|
||
.stat-card {{
|
||
background: white;
|
||
padding: 20px;
|
||
border-radius: 10px;
|
||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||
}}
|
||
.stat-card h3 {{
|
||
margin: 0 0 10px 0;
|
||
color: #666;
|
||
font-size: 14px;
|
||
}}
|
||
.stat-card .value {{
|
||
font-size: 32px;
|
||
font-weight: bold;
|
||
color: #333;
|
||
}}
|
||
.stat-card.success .value {{ color: #10b981; }}
|
||
.stat-card.failed .value {{ color: #ef4444; }}
|
||
.stat-card.skipped .value {{ color: #f59e0b; }}
|
||
.stat-card.error .value {{ color: #8b5cf6; }}
|
||
.section {{
|
||
background: white;
|
||
padding: 20px;
|
||
border-radius: 10px;
|
||
margin-bottom: 20px;
|
||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||
}}
|
||
.section h2 {{
|
||
margin-top: 0;
|
||
color: #333;
|
||
border-bottom: 2px solid #667eea;
|
||
padding-bottom: 10px;
|
||
}}
|
||
.test-result {{
|
||
padding: 15px;
|
||
margin: 10px 0;
|
||
border-radius: 5px;
|
||
border-left: 4px solid;
|
||
}}
|
||
.test-result.passed {{ background: #f0fdf4; border-color: #10b981; }}
|
||
.test-result.failed {{ background: #fef2f2; border-color: #ef4444; }}
|
||
.test-result.skipped {{ background: #fffbeb; border-color: #f59e0b; }}
|
||
.test-result.error {{ background: #f5f3ff; border-color: #8b5cf6; }}
|
||
.progress-bar {{
|
||
width: 100%;
|
||
height: 20px;
|
||
background: #e5e7eb;
|
||
border-radius: 10px;
|
||
overflow: hidden;
|
||
}}
|
||
.progress-bar .fill {{
|
||
height: 100%;
|
||
background: linear-gradient(90deg, #10b981 0%, #10b981 {metrics.success_rate}%, #ef4444 {metrics.success_rate}%, #ef4444 100%);
|
||
transition: width 0.3s ease;
|
||
}}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="header">
|
||
<h1>测试执行报告</h1>
|
||
<p>{metrics.suite_name}</p>
|
||
<p>执行时间: {metrics.start_time} - {metrics.end_time}</p>
|
||
</div>
|
||
|
||
<div class="stats-grid">
|
||
<div class="stat-card">
|
||
<h3>总测试数</h3>
|
||
<div class="value">{metrics.total_tests}</div>
|
||
</div>
|
||
<div class="stat-card success">
|
||
<h3>通过 ✅</h3>
|
||
<div class="value">{metrics.passed}</div>
|
||
</div>
|
||
<div class="stat-card failed">
|
||
<h3>失败 ❌</h3>
|
||
<div class="value">{metrics.failed}</div>
|
||
</div>
|
||
<div class="stat-card skipped">
|
||
<h3>跳过 ⏭️</h3>
|
||
<div class="value">{metrics.skipped}</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="section">
|
||
<h2>成功率</h2>
|
||
<div class="progress-bar">
|
||
<div class="fill"></div>
|
||
</div>
|
||
<p style="text-align: center; margin-top: 10px; font-size: 24px; font-weight: bold;">
|
||
{metrics.success_rate:.2f}%
|
||
</p>
|
||
</div>
|
||
|
||
<div class="section">
|
||
<h2>时间统计</h2>
|
||
<p><strong>总耗时:</strong> {metrics.total_duration:.2f}s</p>
|
||
<p><strong>平均耗时:</strong> {metrics.avg_duration:.2f}s</p>
|
||
</div>
|
||
</body>
|
||
</html>
|
||
"""
|
||
return html
|
||
|
||
def save_html_report(self, metrics: TestSuiteMetrics, output_file: str = None):
|
||
"""保存HTML报告"""
|
||
if output_file is None:
|
||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||
output_file = f"test-report_{timestamp}.html"
|
||
|
||
html = self.generate_html_report(metrics)
|
||
output_path = Path(output_file)
|
||
output_path.write_text(html, encoding='utf-8')
|
||
|
||
return str(output_path)
|
||
|
||
|
||
# 全局指标收集器实例
|
||
_collector: Optional[MetricsCollector] = None
|
||
|
||
|
||
def get_collector() -> MetricsCollector:
|
||
"""获取全局指标收集器"""
|
||
global _collector
|
||
if _collector is None:
|
||
_collector = MetricsCollector()
|
||
return _collector
|
||
|
||
|
||
def reset_collector():
|
||
"""重置指标收集器"""
|
||
global _collector
|
||
_collector = None
|