以下是我的XML示例。有很多这种类似的情况。XML解析子标签
<suite name="regression_1">
<test name="Login check" id="s1-t1">
<tc name="Valid Username & Password">
<message level="INFO" timestamp="20170726 14:25:39.778">Return: None</message>
<status starttime="20170726 14:25:39.740" status="PASS"/>
</tc>
<tc name="Invalid Username or Password">
<message level="INFO" timestamp="20170726 14:25:39.779">Return error</message>
<tc name="Invalid password" library="avi_lib">
<message level="TRACE" timestamp="20170726 14:25:47.769">Return error</message>
<status starttime="20170726 14:25:39.779" status="FAIL"/>
</tc>
<status starttime="20170726 14:25:39.738" status="FAIL"/>
</tc>
<status status="FAIL"/>
</test>
</suite>
我的要求: 通过XML日志,记下测试,测试用例和测试案例状态。如果状态失败,则显示哪个测试用例和测试套件失败以及其他相关消息。
问题我正在面临:我正在迭代测试,收集所有的子测试状态和状态。在下面的代码中,如果tc#2失败,则输出为tc1提供,因为我通过收集列表中的所有状态来迭代tc1。所以输出重复。
我的期望输出(仅适用于状态= “失败”)
测试名称:登录检查
测试用例:无效的用户名&密码
状态:失败
消息:返回错误
以下是我的代码:
# !/usr/bin/python
from xml.dom.minidom import parse
import xml.dom.minidom
import time
DOMTree = xml.dom.minidom.parse("output.xml")
collection = DOMTree.documentElement
tc_entry = collection.getElementsByTagName("suite")
for tc in tc_entry:
if tc.hasAttribute("name"):
print ("Suite name: {}".format(tc.getAttribute("name")))
tests = tc.getElementsByTagName('test')
for test in tests:
testcases = test.getElementsByTagName('tc')
for tc_name in testcases:
status = tc_name.getElementsByTagName('status')
for state in status:
if state.getAttribute("status") != "PASS":
print("Failed")
print("Test name: {}".format(test.getAttribute("name")))
print("Test case name: {}".format(tc_name.getAttribute("name")))
print("Status: {}".format(state.getAttribute("status")))
你可以在两个代码块中发布你想要的输出和你当前的输出吗? – Harrichael