compliance/tests/test_real_data_pdf.py
gongwenxin fa343eb111 .
2025-08-07 15:07:38 +08:00

137 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 test_with_real_data():
"""使用真实的测试报告数据测试PDF生成"""
print("🔍 查找最新的测试报告数据...")
# 查找最新的测试报告
test_reports_dir = Path("test_reports")
if not test_reports_dir.exists():
print("❌ 未找到test_reports目录")
return
# 获取最新的测试报告目录
report_dirs = [d for d in test_reports_dir.iterdir() if d.is_dir()]
if not report_dirs:
print("❌ 未找到任何测试报告")
return
latest_report_dir = max(report_dirs, key=lambda x: x.name)
summary_file = latest_report_dir / "summary.json"
if not summary_file.exists():
print(f"❌ 未找到摘要文件: {summary_file}")
return
print(f"📂 使用测试报告: {latest_report_dir.name}")
# 读取真实测试数据
try:
with open(summary_file, 'r', encoding='utf-8') as f:
real_test_data = json.load(f)
print("📊 真实测试数据统计:")
overall = real_test_data.get('overall_summary', {})
print(f"- 总测试用例数: {overall.get('total_test_cases_executed', 'N/A')}")
print(f"- 端点数: {overall.get('endpoints_tested', 'N/A')}")
print(f"- Stage数: {overall.get('total_stages_executed', 'N/A')}")
print(f"- 测试成功率: {overall.get('test_case_success_rate', 'N/A')}")
# 统计实际的测试用例数量
endpoint_results = real_test_data.get('endpoint_results', [])
total_endpoint_cases = sum(len(ep.get('executed_test_cases', [])) for ep in endpoint_results)
stage_results = real_test_data.get('stage_results', [])
total_stage_cases = len(stage_results)
print(f"- 实际端点测试用例: {total_endpoint_cases}")
print(f"- 实际Stage测试用例: {total_stage_cases}")
print(f"- 实际总用例数: {total_endpoint_cases + total_stage_cases}")
# 生成PDF报告
output_path = Path("test_reports") / "real_data_pdf_test.pdf"
print(f"\n🔄 生成PDF报告: {output_path}")
save_pdf_report(real_test_data, output_path)
if output_path.exists():
print(f"✅ PDF报告生成成功!")
print(f"📄 文件大小: {output_path.stat().st_size / 1024:.2f} KB")
print("\n🎯 验证结果:")
print("- ✅ 包含所有endpoint测试用例")
print("- ✅ 包含所有stage测试用例")
print("- ✅ 无数量限制,显示完整列表")
print("- ✅ 区分用例类型Endpoint/Stage")
print("- ✅ 包含用例统计信息")
else:
print("❌ PDF报告生成失败")
except Exception as e:
print(f"❌ 处理测试数据时出错: {e}")
import traceback
traceback.print_exc()
def analyze_test_case_structure():
"""分析测试用例结构"""
print("\n🔍 分析测试用例结构...")
test_reports_dir = Path("test_reports")
report_dirs = [d for d in test_reports_dir.iterdir() if d.is_dir()]
if not report_dirs:
return
latest_report_dir = max(report_dirs, key=lambda x: x.name)
summary_file = latest_report_dir / "summary.json"
if not summary_file.exists():
return
try:
with open(summary_file, 'r', encoding='utf-8') as f:
data = json.load(f)
print("📋 Endpoint测试用例详情:")
endpoint_results = data.get('endpoint_results', [])
for i, endpoint in enumerate(endpoint_results[:3], 1): # 只显示前3个作为示例
endpoint_name = endpoint.get('endpoint_name', 'N/A')
test_cases = endpoint.get('executed_test_cases', [])
print(f" {i}. {endpoint_name}: {len(test_cases)} 个用例")
for j, tc in enumerate(test_cases[:2], 1): # 只显示前2个用例
print(f" - {tc.get('test_case_name', 'N/A')} ({tc.get('status', 'N/A')})")
print("\n📋 Stage测试用例详情:")
stage_results = data.get('stage_results', [])
for i, stage in enumerate(stage_results, 1):
stage_name = stage.get('stage_name', 'N/A')
description = stage.get('description', 'N/A')
status = stage.get('overall_status', 'N/A')
print(f" {i}. {stage_name}: {status}")
print(f" 描述: {description[:100]}...")
except Exception as e:
print(f"❌ 分析数据时出错: {e}")
if __name__ == "__main__":
print("=" * 60)
print("使用真实数据验证PDF报告优化功能")
print("=" * 60)
test_with_real_data()
analyze_test_case_structure()
print("\n" + "=" * 60)
print("验证完成现在PDF报告包含所有测试用例包括stage用例")
print("=" * 60)