241 lines
10 KiB
Python
241 lines
10 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试优化后的PDF报告生成功能
|
||
"""
|
||
|
||
import json
|
||
import sys
|
||
from pathlib import Path
|
||
import datetime
|
||
|
||
# 添加项目根目录到Python路径
|
||
sys.path.insert(0, str(Path(__file__).parent))
|
||
|
||
from run_api_tests import save_pdf_report
|
||
|
||
def create_sample_test_data():
|
||
"""创建示例测试数据,包含40个测试用例和stage用例"""
|
||
return {
|
||
"start_time": "2025-07-30T10:00:00.000000",
|
||
"end_time": "2025-07-30T10:05:30.500000",
|
||
"duration_seconds": "330.50",
|
||
"overall_summary": {
|
||
"total_endpoints_defined": 15,
|
||
"endpoints_tested": 12,
|
||
"endpoints_passed": 10,
|
||
"endpoints_failed": 2,
|
||
"endpoints_error": 0,
|
||
"endpoints_skipped": 3,
|
||
"endpoint_success_rate": "83.33%",
|
||
"total_test_cases_applicable": 200,
|
||
"total_test_cases_executed": 40, # 模拟40个测试用例
|
||
"test_cases_passed": 28,
|
||
"test_cases_failed": 12,
|
||
"test_cases_error": 0,
|
||
"test_case_success_rate": "70.00%",
|
||
"total_stages_defined": 1,
|
||
"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"
|
||
}
|
||
]
|
||
},
|
||
{
|
||
"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-001",
|
||
"test_case_name": "基本状态码 200 检查",
|
||
"test_case_severity": "CRITICAL",
|
||
"status": "通过",
|
||
"message": "响应状态码为 200,符合预期。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-CRUD-001",
|
||
"test_case_name": "CRUD操作验证",
|
||
"test_case_severity": "HIGH",
|
||
"status": "通过",
|
||
"message": "新增、删除、修改、查询操作正常。"
|
||
}
|
||
]
|
||
},
|
||
{
|
||
"endpoint_id": "POST_/api/dms/wb_ml/v1/log_parsing",
|
||
"endpoint_name": "测井曲线解析服务",
|
||
"overall_status": "通过",
|
||
"duration_seconds": 0.312,
|
||
"executed_test_cases": [
|
||
{
|
||
"test_case_id": "TC-STATUS-001",
|
||
"test_case_name": "基本状态码 200 检查",
|
||
"test_case_severity": "CRITICAL",
|
||
"status": "通过",
|
||
"message": "响应状态码为 200,符合预期。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-PARSING-001",
|
||
"test_case_name": "数据解析功能验证",
|
||
"test_case_severity": "HIGH",
|
||
"status": "通过",
|
||
"message": "成功解析wis、las格式的测井数据。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-SECURITY-001",
|
||
"test_case_name": "HTTPS协议强制性检查",
|
||
"test_case_severity": "HIGH",
|
||
"status": "失败",
|
||
"message": "API通过HTTP响应,违反HTTPS强制策略。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-RESTful-001",
|
||
"test_case_name": "核心命名与结构规范检查",
|
||
"test_case_severity": "HIGH",
|
||
"status": "失败",
|
||
"message": "响应中包含一个主列表,但其键名不符合规范。"
|
||
}
|
||
]
|
||
},
|
||
{
|
||
"endpoint_id": "GET_/api/dms/wb_ml/v1/well_info/{id}",
|
||
"endpoint_name": "井信息详情查询服务",
|
||
"overall_status": "通过",
|
||
"duration_seconds": 0.156,
|
||
"executed_test_cases": [
|
||
{
|
||
"test_case_id": "TC-STATUS-001",
|
||
"test_case_name": "基本状态码 200 检查",
|
||
"test_case_severity": "CRITICAL",
|
||
"status": "通过",
|
||
"message": "响应状态码为 200,符合预期。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-PARAM-001",
|
||
"test_case_name": "路径参数验证",
|
||
"test_case_severity": "HIGH",
|
||
"status": "通过",
|
||
"message": "路径参数ID验证通过。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-RESPONSE-001",
|
||
"test_case_name": "响应数据格式验证",
|
||
"test_case_severity": "HIGH",
|
||
"status": "通过",
|
||
"message": "响应数据格式符合Schema定义。"
|
||
}
|
||
]
|
||
},
|
||
{
|
||
"endpoint_id": "PUT_/api/dms/wb_ml/v1/well_info",
|
||
"endpoint_name": "井信息更新服务",
|
||
"overall_status": "失败",
|
||
"duration_seconds": 0.234,
|
||
"executed_test_cases": [
|
||
{
|
||
"test_case_id": "TC-STATUS-001",
|
||
"test_case_name": "基本状态码 200 检查",
|
||
"test_case_severity": "CRITICAL",
|
||
"status": "失败",
|
||
"message": "响应状态码为 500,预期为 200。"
|
||
},
|
||
{
|
||
"test_case_id": "TC-UPDATE-001",
|
||
"test_case_name": "数据更新验证",
|
||
"test_case_severity": "HIGH",
|
||
"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,
|
||
"start_time": "2025-07-30T10:02:00.000000",
|
||
"end_time": "2025-07-30T10:02:02.450000"
|
||
},
|
||
{
|
||
"stage_id": "keyword_driven_crud_stage",
|
||
"stage_name": "Keyword Driven CRUD Stage",
|
||
"description": "基于关键字驱动的CRUD操作测试阶段",
|
||
"overall_status": "通过",
|
||
"duration_seconds": 1.85,
|
||
"start_time": "2025-07-30T10:03:00.000000",
|
||
"end_time": "2025-07-30T10:03:01.850000"
|
||
}
|
||
]
|
||
}
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("开始测试优化后的PDF报告生成功能...")
|
||
|
||
# 创建测试数据
|
||
test_data = create_sample_test_data()
|
||
|
||
# 设置输出路径
|
||
output_path = Path("test_reports") / "optimized_report_test.pdf"
|
||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||
|
||
try:
|
||
# 生成PDF报告 - 测试不同的严格等级
|
||
save_pdf_report(test_data, output_path, 'HIGH') # 使用HIGH级别测试
|
||
|
||
if output_path.exists():
|
||
print(f"✅ PDF报告生成成功: {output_path}")
|
||
print(f"📄 文件大小: {output_path.stat().st_size / 1024:.2f} KB")
|
||
print("\n📊 测试数据统计:")
|
||
print(f"- 总测试用例数: {test_data['overall_summary']['total_test_cases_executed']}")
|
||
print(f"- 端点测试用例: {sum(len(ep.get('executed_test_cases', [])) for ep in test_data['endpoint_results'])}")
|
||
print(f"- Stage测试用例: {len(test_data['stage_results'])}")
|
||
print(f"- 测试成功率: {test_data['overall_summary']['test_case_success_rate']}")
|
||
print("\n📋 报告包含以下优化内容:")
|
||
print("- 报告编码和基本信息")
|
||
print("- 摘要部分")
|
||
print("- API服务列表表格")
|
||
print("- 完整测试用例列表(包含所有40个用例和stage用例)")
|
||
print("- 测试情况说明")
|
||
print("- 测试结论")
|
||
print("- 检测依据")
|
||
print("- 报告生成信息")
|
||
else:
|
||
print("❌ PDF报告生成失败")
|
||
|
||
except Exception as e:
|
||
print(f"❌ 生成PDF报告时出错: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
if __name__ == "__main__":
|
||
main()
|