Python 的 xml.dom.minidom 中的 getElementsByTagName() 不工作

getElementsByTagName() in Python's xml.dom.minidom is not working

我正在解析从 gtest 生成的输出 XML 文件。我想找到每个测试用例的结果。仅当 "testcase" 具有元素 "failure" 时测试用例失败,否则测试用例通过。但是我无法访问元素。

我的 xml 文件:-

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="11" failures="0" disabled="0" errors="0" timestamp="2015-03-23T17:29:43" time="1.309" name="AllTests">
  <testsuite name="AAA" tests="4" failures="0" disabled="0" errors="0" time="0.008">
    <testcase name="BBBB" status="run" time="0.002" classname="AAA" />
      <failure message="Value of: add(1, 1)&#x0A; Actual: 3&#x0A;Expected: 2" type="" />
    <testcase name="CCC" status="run" time="0.002" classname="AAA" />
    <testcase name="DDD" status="run" time="0.002" classname="AAA" />
    <testcase name="FFF" status="run" time="0.002" classname="AAA" />
  </testsuite>
</testsuites>

我的 python 文件是:-

from xlrd import open_workbook
from xml.dom.minidom import parse
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("output.xml")
testsuites = DOMTree.documentElement 
testCaseCollection = testsuites.getElementsByTagName("testcase")
testCasefailure = testsuites.getElementsByTagName("failure")

OutputXLS = open_workbook('output.xls')

for testCase in testCaseCollection:

        #print testCase.firstChild;
        if testsuites.getElementsByTagName("failure"):
                print testCase.getAttribute("name"), " --> ","FAIL"
        else:
                print testCase.getAttribute("name"), " --> ","PASS"

输出为:-

BBB -->  PASS
CCC  -->  PASS
DDD  -->  PASS
FFF  -->  PASS

虽然测试用例"BBB"失败,因为它在xml中有"failure"属性,但结果显示通过。 请帮我解决这个问题。

from xlrd import open_workbook
from xml.dom.minidom import parse

# Open XML document using minidom parser
DOMTree = parse("output.xml")
testsuites = DOMTree.documentElement 
testCaseCollection = testsuites.getElementsByTagName("testcase")

OutputXLS = open_workbook('output.xls')

for testCase in testCaseCollection:
    sibNode = testCase.nextSibling.nextSibling
    if sibNode and sibNode.nodeName == 'failure':
        print testCase.getAttribute("name"), " --> ","FAIL"
    else:
        print testCase.getAttribute("name"), " --> ","PASS"