我正在运行android测试,它输出一个通用的JUnit XML报告,如下所示。我正试图在理想情况下找到Python扩展或简易方法,以便将这些结果打印到控制台。
我能够用Python插件将XML转换成html,这在某些情况下很有用。但这并不理想,因为它需要额外的步骤来打开,并且需要一个GUI,而这并不总是可能的。我可以直接打印XML,但它不是很干净。我们想要一份直接的清洁打印。
例如,这是我的XML文件
<?xml version="1.0"?>
<testsuites>
<testsuite name="My test suite 1" tests="2" failures="0" skipped="0" timedout="0" errors="0" time="316.032" timestamp="2022-11-10 21:43:40 +0000">
<properties>
<property name="id" value="28394"/>
<property name="device" value="Samsung Galaxy S10"/>
</properties>
<testcase name="test library" classname="library" result="passed" test_id="1" time="7.775"/>
<testcase name="test package" classname="package" result="passed" test_id="2" time="7.986"/>
</testsuite>
<testsuite name="test suite 2" tests="1" failures="1" skipped="0" timedout="0" errors="0" time="193.795" timestamp="2022-11-10 21:55:10 +0000">
<properties>
<property name="id" value="239548"/>
<property name="device" value="Samsung Galaxy S10"/>
</properties>
<testcase name="test API" classname="apiTest" result="failed" test_id="1" time="193.795" >
<failure>
Failure message will be properly filled in
</failure>
</testcase>
</testsuite>
</testsuites>
... 我想把它打印如下
My test suite 1
timedout=0
timestamp=2022-11-10 21:43:40 +0000
id=28394
device=Samsung Galaxy S10
✓ test library time=7.775
✓ test package time=7.986
test suite 2
timedout=0
timestamp=2022-11-10 21:55:10 +0000
id=239548
device=Samsung Galaxy S10
✗ test API time=193.795
- Failure message will be properly filled in
2 passed, 1 failure到目前为止,我尝试的是;
--console完美地工作在一起,这正是我想要的。但是,我更愿意使用python包,因为它更容易确保多个系统具有相同的设置,方法是共享一个pipenv并允许所有东西在python中运行。而不必安装npm,后面跟着这个包,然后在外部运行。junit2html创建一个具有输出摘要矩阵选项的html文件,但这不包括详细信息,比如故障日志,只是一个简短的摘要。junit_xml执行xml=JUnitXml.fromfile(<FILE>),然后执行xml.tostring(),但是这会以XML格式打印出来,而不是一个很好的输出。import xml.etree.ElementTree as ET并迭代元素以自定义打印它们。但是,我更愿意使用一个定义的模块来完成这个任务,因为我可能没有涵盖所有可能的场景,等等,而这些场景可能不在我的文件中。发布于 2022-11-16 12:27:45
编写自定义代码来处理这种情况
def main(xml_path):
tree = ET.parse(xml_path)
root = tree.getroot()
for child in root.iter():
if child.tag == "testsuite":
# Add code for all cases, via `child.attrib or child.text`https://stackoverflow.com/questions/74424877
复制相似问题