如何从Robot Framework的report.html提取套件名、用例名及跳过信息
从Robot Framework的report.html提取跳过用例相关信息的实现方法
下面几种方法可以实现你的需求,按需选择即可:
方法一:Python + BeautifulSoup解析HTML(灵活定制)
这是最适合定制化需求的方式,步骤如下:
- 先安装依赖包:
pip install beautifulsoup4 - 编写Python脚本解析本地报告文件:
from bs4 import BeautifulSoup # 读取本地的report.html文件 with open("report.html", "r", encoding="utf-8") as f: soup = BeautifulSoup(f.read(), "html.parser") # 定位所有标记为跳过的测试用例节点 skipped_cases = soup.find_all("tr", {"status": "SKIP"}) for case in skipped_cases: # 提取测试用例名称 case_name = case.find("td", class_="name").text.strip() # 提取并格式化跳过消息(把Skip Defect#xxx转成Defect_xxx) skip_msg = case.find("td", class_="message").text.strip() defect_id = skip_msg.replace("Skip Defect#", "Defect_") # 提取所属套件名 suite_name = case.find_parent("div", class_="suite").find("h2").text.strip() print(f"套件名: {suite_name}, 用例名: {case_name}, 缺陷编号: {defect_id}")
方法二:用Robot Framework自带工具生成结构化数据
利用rebot工具把HTML报告转成JSON格式,再解析会更简单:
- 执行命令生成JSON格式报告:
rebot --outputjson skipped_cases.json report.html - 用Python解析JSON文件:
import json with open("skipped_cases.json", "r", encoding="utf-8") as f: report_data = json.load(f) # 递归遍历套件和用例,提取跳过的目标信息 def get_skipped_info(suite): current_suite = suite["name"] # 处理当前套件下的测试用例 for test_case in suite.get("tests", []): if test_case["status"] == "SKIP": case_name = test_case["name"] defect_id = test_case["message"].replace("Skip Defect#", "Defect_") print(f"套件名: {current_suite}, 用例名: {case_name}, 缺陷编号: {defect_id}") # 处理子套件 for sub_suite in suite.get("suites", []): get_skipped_info(sub_suite) get_skipped_info(report_data["suite"])
方法三:命令行工具快速提取(适合简单场景)
如果只是临时快速提取,用Linux/macOS的命令行工具就能搞定:
grep -A 2 -B 1 '"SKIP"' report.html | awk '/<td class="name">/{case=$0; gsub(/<[^>]+>/,"",case);} /Skip Defect#/{msg=$0; gsub(/<[^>]+>|\sSkip /,"",msg); gsub(/#/,"_",msg);} /<h2>/{suite=$0; gsub(/<[^>]+>/,"",suite);} case && msg && suite{print "套件名:" suite " 用例名:" case " 缺陷编号:" msg; case=msg=suite=""}'
内容的提问来源于stack exchange,提问作者Nanda
相关产品推荐
相关产品推荐

