206 lines
8.0 KiB
Python
206 lines
8.0 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试严格等级功能的PDF报告生成
|
||
"""
|
||
|
||
import json
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# 添加项目根目录到Python路径
|
||
sys.path.insert(0, str(Path(__file__).parent))
|
||
|
||
from run_api_tests import save_pdf_report
|
||
|
||
def create_test_data_with_mixed_severity():
|
||
"""创建包含不同严重性等级测试用例的数据"""
|
||
return {
|
||
"start_time": "2025-07-31T10:00:00.000000",
|
||
"end_time": "2025-07-31T10:05:30.500000",
|
||
"duration_seconds": "330.50",
|
||
"overall_summary": {
|
||
"total_endpoints_defined": 10,
|
||
"endpoints_tested": 8,
|
||
"endpoints_passed": 6,
|
||
"endpoints_failed": 2,
|
||
"endpoints_error": 0,
|
||
"endpoints_skipped": 2,
|
||
"endpoint_success_rate": "75.00%",
|
||
"total_test_cases_applicable": 120,
|
||
"total_test_cases_executed": 45,
|
||
"test_cases_passed": 32,
|
||
"test_cases_failed": 13,
|
||
"test_cases_error": 0,
|
||
"test_case_success_rate": "71.11%",
|
||
"total_stages_defined": 2,
|
||
"total_stages_executed": 2,
|
||
"stages_passed": 2,
|
||
"stages_failed": 0,
|
||
"stages_error": 0,
|
||
"stages_skipped": 0,
|
||
"stage_success_rate": "100.00%"
|
||
},
|
||
"endpoint_results": [
|
||
{
|
||
"endpoint_id": "POST_/api/dms/wb_ml/v1/well_info",
|
||
"endpoint_name": "井信息查询服务",
|
||
"overall_status": "通过",
|
||
"duration_seconds": 0.245,
|
||
"executed_test_cases": [
|
||
{
|
||
"test_case_id": "TC-STATUS-001",
|
||
"test_case_name": "基本状态码 200 检查",
|
||
"test_case_severity": "CRITICAL",
|
||
"status": "通过",
|
||
"message": "响应状态码为 200,符合预期。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-HEADER-001",
|
||
"test_case_name": "必需请求头Schema验证",
|
||
"test_case_severity": "HIGH",
|
||
"status": "失败",
|
||
"message": "缺少必需的请求头 X-Tenant-ID"
|
||
},
|
||
{
|
||
"test_case_id": "TC-RESPONSE-001",
|
||
"test_case_name": "响应格式验证",
|
||
"test_case_severity": "MEDIUM",
|
||
"status": "通过",
|
||
"message": "响应格式符合规范"
|
||
},
|
||
{
|
||
"test_case_id": "TC-PERF-001",
|
||
"test_case_name": "响应时间检查",
|
||
"test_case_severity": "LOW",
|
||
"status": "失败",
|
||
"message": "响应时间超过预期"
|
||
},
|
||
{
|
||
"test_case_id": "TC-INFO-001",
|
||
"test_case_name": "信息完整性检查",
|
||
"test_case_severity": "INFO",
|
||
"status": "通过",
|
||
"message": "信息完整"
|
||
}
|
||
]
|
||
},
|
||
{
|
||
"endpoint_id": "POST_/api/dms/wb_ml/v1/layer_info",
|
||
"endpoint_name": "分层信息表查询服务",
|
||
"overall_status": "失败",
|
||
"duration_seconds": 0.189,
|
||
"executed_test_cases": [
|
||
{
|
||
"test_case_id": "TC-STATUS-002",
|
||
"test_case_name": "基本状态码 200 检查",
|
||
"test_case_severity": "CRITICAL",
|
||
"status": "失败",
|
||
"message": "响应状态码为 500,预期为 200"
|
||
},
|
||
{
|
||
"test_case_id": "TC-CRUD-001",
|
||
"test_case_name": "CRUD操作验证",
|
||
"test_case_severity": "HIGH",
|
||
"status": "通过",
|
||
"message": "CRUD操作正常"
|
||
},
|
||
{
|
||
"test_case_id": "TC-VALIDATE-001",
|
||
"test_case_name": "数据验证检查",
|
||
"test_case_severity": "MEDIUM",
|
||
"status": "失败",
|
||
"message": "数据验证失败"
|
||
}
|
||
]
|
||
}
|
||
],
|
||
"stage_results": [
|
||
{
|
||
"stage_id": "dms_crud_scenario_stage",
|
||
"stage_name": "DMS Full CRUD Scenario",
|
||
"description": "执行完整的Create->Read->Update->Delete->List工作流测试",
|
||
"overall_status": "通过",
|
||
"duration_seconds": 2.45
|
||
},
|
||
{
|
||
"stage_id": "keyword_driven_crud_stage",
|
||
"stage_name": "Keyword Driven CRUD Stage",
|
||
"description": "基于关键字驱动的CRUD操作测试阶段",
|
||
"overall_status": "通过",
|
||
"duration_seconds": 1.85
|
||
}
|
||
]
|
||
}
|
||
|
||
def test_different_strictness_levels():
|
||
"""测试不同严格等级的PDF报告生成"""
|
||
print("🧪 测试不同严格等级的PDF报告生成...")
|
||
|
||
test_data = create_test_data_with_mixed_severity()
|
||
|
||
# 测试不同的严格等级
|
||
strictness_levels = ['CRITICAL', 'HIGH', 'MEDIUM', 'LOW']
|
||
|
||
for level in strictness_levels:
|
||
print(f"\n📊 测试严格等级: {level}")
|
||
|
||
output_path = Path("test_reports") / f"strictness_{level.lower()}_test.pdf"
|
||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||
|
||
try:
|
||
save_pdf_report(test_data, output_path, level)
|
||
|
||
if output_path.exists():
|
||
print(f"✅ {level}级别PDF报告生成成功: {output_path}")
|
||
print(f"📄 文件大小: {output_path.stat().st_size / 1024:.2f} KB")
|
||
|
||
# 分析该级别下的必须/非必须用例分布
|
||
severity_values = {'CRITICAL': 5, 'HIGH': 4, 'MEDIUM': 3, 'LOW': 2, 'INFO': 1}
|
||
strictness_value = severity_values.get(level, 5)
|
||
|
||
# 统计测试用例
|
||
total_cases = 0
|
||
required_cases = 0
|
||
|
||
for endpoint in test_data['endpoint_results']:
|
||
for tc in endpoint.get('executed_test_cases', []):
|
||
total_cases += 1
|
||
tc_severity = tc.get('test_case_severity', 'MEDIUM')
|
||
tc_value = severity_values.get(tc_severity, 3)
|
||
if tc_value >= strictness_value:
|
||
required_cases += 1
|
||
|
||
# 加上Stage用例(默认HIGH级别)
|
||
stage_cases = len(test_data.get('stage_results', []))
|
||
total_cases += stage_cases
|
||
if severity_values.get('HIGH', 4) >= strictness_value:
|
||
required_cases += stage_cases
|
||
|
||
optional_cases = total_cases - required_cases
|
||
|
||
print(f" 📋 用例分布: 必须 {required_cases} 个,非必须 {optional_cases} 个")
|
||
|
||
else:
|
||
print(f"❌ {level}级别PDF报告生成失败")
|
||
|
||
except Exception as e:
|
||
print(f"❌ 生成{level}级别PDF报告时出错: {e}")
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("=" * 70)
|
||
print("测试严格等级功能和更新后的摘要")
|
||
print("=" * 70)
|
||
|
||
test_different_strictness_levels()
|
||
|
||
print("\n" + "=" * 70)
|
||
print("🎯 功能验证完成!")
|
||
print("✅ 摘要现在包含端点通过数量数据")
|
||
print("✅ 测试用例根据严格等级正确分离为必须和非必须")
|
||
print("✅ 不同严格等级生成不同的用例分布")
|
||
print("=" * 70)
|
||
|
||
if __name__ == "__main__":
|
||
main()
|