TBC/tests/conftest_metrics.py
d-robotics 633a797226 AutoFlow v2.0: 合并 Test (Robogo) + data-loop-automation (D-Cloud) 为统一平台
- 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 模块
2026-06-23 11:48:29 +08:00

200 lines
5.9 KiB
Python

"""
Pytest 指标收集插件
自动收集测试执行指标,无需手动调用
"""
import pytest
from typing import List
from tests.utils.metrics import (
get_collector,
MetricsReporter,
TestStatus,
)
from tests.utils.flaky_detector import get_registry
def pytest_configure(config):
"""pytest 配置钩子"""
# 注册自定义标记
config.addinivalue_line(
"markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')"
)
config.addinivalue_line(
"markers", "smoke: marks tests as smoke tests"
)
config.addinivalue_line(
"markers", "integration: marks tests as integration tests"
)
config.addinivalue_line(
"markers", "flaky: marks tests as flaky (may need retry)"
)
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
"""测试开始前的钩子"""
collector = get_collector()
# 获取测试信息
test_id = item.nodeid
test_name = item.name
file_path = str(item.fspath)
class_name = ""
# 获取类名(如果有)
if item.cls:
class_name = item.cls.__name__
# 获取 markers
markers = [m.name for m in item.iter_markers()]
# 开始测试
collector.start_test(
test_id=test_id,
test_name=test_name,
file_path=file_path,
class_name=class_name,
markers=markers
)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""测试报告生成钩子"""
outcome = yield
report = outcome.get_result()
collector = get_collector()
registry = get_registry()
# 只在测试调用阶段处理
if report.when == "call":
status = None
error_message = None
error_traceback = None
if report.passed:
status = TestStatus.PASSED
elif report.failed:
status = TestStatus.FAILED
# 获取错误信息
error_message = str(call.excinfo.value) if call.excinfo else "Unknown error"
error_traceback = str(call.excinfo.traceback) if call.excinfo else None
elif report.skipped:
status = TestStatus.SKIPPED
if status:
# 结束测试
collector.end_test(
status,
error_message=error_message,
error_traceback=error_traceback
)
# 记录到 Flaky 注册表
test_id = item.nodeid
test_name = item.name
duration = report.duration if hasattr(report, 'duration') else 0.0
registry.record_execution(
test_id=test_id,
test_name=test_name,
status=status.value,
duration=duration,
error_message=error_message
)
# 如果是 Flaky 测试且失败,建议重试
if status == TestStatus.FAILED and registry.is_flaky(test_id):
retry_count = registry.get_retry_count(test_id)
if retry_count > 0:
print(f"\n⚠️ Flaky 测试检测: {test_name}")
print(f" 建议重试次数: {retry_count}")
print(f" 历史成功率: {registry.get_test_history(test_id).success_rate:.2f}%")
elif report.when == "setup":
if report.failed:
# Setup 失败视为错误
error_message = str(call.excinfo.value) if call.excinfo else "Setup failed"
collector.end_test(
TestStatus.ERROR,
error_message=error_message
)
def pytest_sessionstart(session):
"""测试会话开始"""
collector = get_collector()
collector.start_suite(suite_name="Test Suite")
def pytest_sessionfinish(session, exitstatus):
"""测试会话结束"""
collector = get_collector()
registry = get_registry()
metrics = collector.end_suite()
if metrics:
# 生成报告
reporter = MetricsReporter()
# 保存文本报告
txt_report = reporter.save_report(metrics, "test-results/test-report.txt")
print(f"\n📊 测试报告已生成: {txt_report}")
# 保存 HTML 报告
html_report = reporter.save_html_report(metrics, "test-results/test-report.html")
print(f"📊 HTML 报告已生成: {html_report}")
# 生成 Flaky 测试报告
flaky_tests = registry.get_flaky_tests()
if flaky_tests:
flaky_report = registry.save_report()
print(f"\n⚠️ Flaky 测试报告已生成: {flaky_report}")
# 打印摘要
summary = collector.get_summary()
print("\n" + "=" * 80)
print("测试执行摘要")
print("=" * 80)
print(f"总测试数: {summary['total_tests']}")
print(f"通过: {summary['passed']} ✅")
print(f"失败: {summary['failed']} ❌")
print(f"跳过: {summary['skipped']} ⏭️")
print(f"错误: {summary['error']} ⚠️")
print(f"成功率: {summary['success_rate']}")
print(f"总耗时: {summary['total_duration']}")
# Flaky 测试统计
if flaky_tests:
print(f"\n⚠️ Flaky 测试数量: {len(flaky_tests)}")
print("=" * 80)
# Flaky 测试检测装饰器
def flaky(max_retries: int = 2, min_passes: int = 1):
"""
标记 Flaky 测试的装饰器
Args:
max_retries: 最大重试次数
min_passes: 最少通过次数
用法:
@flaky(max_retries=3, min_passes=2)
def test_unstable_feature():
# 可能不稳定的测试
pass
"""
def decorator(func):
# 添加 pytest 标记
func.flaky = {
'max_retries': max_retries,
'min_passes': min_passes
}
return pytest.mark.flaky(max_retries=max_retries, min_passes=min_passes)(func)
return decorator