关于生成符合要求的cargo test HTML报告并导出至Polarion的技术咨询
Absolutely! This is a common workflow in Rust projects that integrate with Polarion for test management, and there are a few solid approaches to pull this off. Let’s break it down step by step:
1. 生成包含Polarion ID的HTML测试报告
Cargo本身并不直接支持生成自定义HTML测试报告,也不会自动提取Polarion ID,但我们可以借助第三方工具和简单的脚本实现这个需求:
方案一:使用cargo-nextest + 自定义HTML渲染
cargo-nextest是Rust生态中流行的测试运行工具,它支持输出结构化的JSON测试结果,我们可以基于这个结果生成包含Polarion ID的HTML报告:
- 首先安装工具:
cargo install cargo-nextest - 运行测试并导出JSON格式的结果:
cargo nextest run --no-fail-fast --output-file test-results.json --format json - 编写脚本解析JSON并生成HTML:
你可以用Python、Rust或任何你熟悉的语言写一个简单的脚本,提取测试用例名称中的Polarion ID(比如用正则匹配TST-\d+这类格式),然后生成带表格的HTML。举个Python脚本的示例:
运行这个脚本后,就能得到包含Polarion ID、测试用例名称和状态的HTML报告了。import json import re # 加载测试结果 with open('test-results.json', 'r') as f: test_data = json.load(f) # 匹配Polarion ID的正则 polarion_pattern = re.compile(r'(TST-\d+)') html_table_rows = [] # 遍历每个测试用例 for test in test_data['tests']: test_name = test['test_name'] status = test['status'].capitalize() # 提取Polarion ID,没有的话标记为N/A polarion_id_match = polarion_pattern.search(test_name) polarion_id = polarion_id_match.group(1) if polarion_id_match else 'N/A' html_table_rows.append(f"<tr><td>{polarion_id}</td><td>{test_name}</td><td>{status}</td></tr>") # 组装完整HTML内容 final_html = f""" <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Cargo Test Report (with Polarion IDs)</title> <style> table {{ border-collapse: collapse; width: 80%; margin: 20px auto; }} th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }} th {{ background-color: #f2f2f2; }} h1 {{ text-align: center; color: #333; }} </style> </head> <body> <h1>Rust Cargo Test Results</h1> <table> <tr><th>Polarion ID</th><th>Test Case Name</th><th>Status</th></tr> {''.join(html_table_rows)} </table> </body> </html> """ # 写入HTML文件 with open('cargo-test-report.html', 'w') as f: f.write(final_html)
方案二:使用自定义测试报告Crate
Rust生态中有一些专门用于生成测试报告的Crate,它们支持自定义报告格式,也能从测试属性或名称中提取Polarion ID。比如,你可以在测试代码中添加标注属性:
#[test] #[cfg_attr(test, note = "Polarion: TST-789")] fn test_checkout_process() { // 测试逻辑 }
这类工具会读取这些属性,直接生成包含Polarion信息的HTML报告,你可以通过cargo install安装对应的Crate后,按照文档配置使用。
2. 将测试报告导出至Polarion
拿到测试结果(不管是JSON源数据还是HTML报告的结构化信息)后,有两种可靠的方式同步到Polarion:
方案一:调用Polarion REST API
Polarion提供了REST API用于管理测试用例和测试运行,你可以写一个脚本完成以下操作:
- 解析测试结果中的Polarion ID、测试名称和状态
- 调用API验证Polarion中是否存在对应的测试用例
- 创建或复用一个测试运行实例
- 将每个测试的状态(Passed/Failed/Skipped)同步到Polarion中对应的测试用例
你可以用任何支持HTTP请求的语言实现这个脚本,比如用Python的requests库处理认证和API请求,确保使用项目的Polarion账号权限进行操作。
方案二:使用Polarion内置导入工具
Polarion支持导入CSV、XML等结构化格式的测试结果。我们可以先从测试JSON结果生成CSV文件,再用Polarion的导入功能批量上传:
- 生成CSV的示例脚本(Python):
生成CSV后,在Polarion的测试管理界面找到导入功能,选择CSV文件,将列映射到Polarion对应的字段(比如Polarion ID对应测试用例ID、Status对应测试结果状态),即可完成批量同步。import json import re with open('test-results.json', 'r') as f: test_data = json.load(f) polarion_pattern = re.compile(r'(TST-\d+)') csv_content = ["Polarion ID,Test Case Name,Status"] for test in test_data['tests']: test_name = test['test_name'] status = test['status'] polarion_id_match = polarion_pattern.search(test_name) polarion_id = polarion_id_match.group(1) if polarion_id_match else 'N/A' csv_content.append(f"{polarion_id},{test_name},{status}") with open('test-results.csv', 'w') as f: f.write('\n'.join(csv_content))
实用小技巧
- 统一测试命名规范:约定测试用例名称格式(比如
test_polarion_TST-123_login_flow),这样提取Polarion ID的逻辑会更可靠,避免匹配错误。 - 集成到CI/CD流程:将测试运行、报告生成、同步到Polarion的步骤集成到你的CI/CD流水线中,实现每次代码提交后自动完成全流程。
- 处理跳过的测试:根据项目的测试管理规范,将跳过的测试映射到Polarion中的“Blocked”或“Skipped”状态,确保结果准确。
内容来源于stack exchange

