In Python, I m using from xml.dom import minidom module. Further, I have an xml, trying to parse it using the above module.
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="4" failures="2">
<testsuite name="iOSUITests" tests="4" failures="2" time="9325.715">
<testcase classname="DR" name="test_1()" time="26.1042">
</testcase>
<testcase classname="DR" name="test_2()" time="31.5843">
</testcase>
<testcase classname="DR" name="test_3()" time="32.8958">
<failure message="failed - reason1">
</failure>
</testcase>
<testcase classname="GD" name="test_4()" time="48.18003">
<failure message="failed - reason2">
</failure>
</testcase>
</testsuite>
</testsuites>
I m trying to get failure child tag of each test case but unable to get it for the corresponding test case. As i need to update test case status as Pass or Fail and its message. I tried to execute my below code.
testcase = doc.getElementsByTagName("testcase")
for index, tc in enumerate(testcase):
xml_testcase_list.append({})
xml_testcase_list[index][classname] = tc.getAttribute(classname)
#print "tc.firstChild.items()", tc.childNodes.item
#print "tc.firstChild.items()", tc.firstChild.nodeValue
if 'failure' in tc.firstChild.nodeValue:
xml_testcase_list[index][failure] = []
failure = doc.getElementsByTagName("failure")[0]
#print "getNodeText(failure)", failure.childNodes
for each_index, each_failure in enumerate(failure):
xml_testcase_list[index][failure].append({})
xml_testcase_list[index][failure][each_index][message] = each_failure.getAttribute(message)
How can i get a failure tag in my if condition?
To get all failure
child tags of a testcase
element, if any exists, you can call tc.getElementsByTagName("failure")
. If you use that method on an element, it will only inspect children. Then you can check if the array returned is empty, and get the first result if it's not.
from xml.dom import minidom
with open('xml.xml') as f: # your file stored in xml.xml
doc = minidom.parse(f)
testcase = doc.getElementsByTagName("testcase")
xml_testcase_list = []
for index, tc in enumerate(testcase):
xml_testcase_list.append({})
xml_testcase_list[index]['classname'] = tc.getAttribute('classname')
failures = tc.getElementsByTagName('failure')
if failures:
xml_testcase_list[index][failure] = []
failure = failures[0]
for each_index, each_failure in enumerate(failure):
xml_testcase_list[index][failure].append({})
xml_testcase_list[index][failure][each_index][message] = each_failure.getAttribute(message)
else:
print 'no failures for', tc
I have to say that your example does not include any child elements of failure
, so the for each_index, each_failure
will not work as intended.