Junit 4test结果解析无法找到<failure>标记

时间:2017-05-30 08:07:07

标签: junit4

这是我的Junit测试result.xml  我能够获得testsuite和testcase节点。现在我需要检查测试用例是否失败。 我正在使用一种方法,我检查testcase节点是否有任何子节点,然后检查该子节点是否是故障节点。但是当我尝试获取子节点及其类型时,它只返回TEXT_NODE并显示失败消息文本。如何检查子节点是否失败?

<?xml version='1.0' encoding='UTF-8' ?>
<testsuite name="com.Test" tests="47" failures="1" errors="0" skipped="0" time="0.425" timestamp="2017-05-25T10:04:14" hostname="localhost">
  <properties>
    <property name="device" value="Nexus_6_API_22(AVD) - 5.1.1" />
    <property name="flavor" value="DEV" />
    <property name="project" value="test" />
  </properties>
  <testcase name="test_1" classname="com.Test" time="0.053" />
  <testcase name="test_2" classname="com.Test" time="0.081" />
  <testcase name="test_3" classname="com.Test" time="0.0" />
  <testcase name="test_4" classname="com.Test" time="0.001">
    <failure>junit.framework.ComparisonFailure: expected:&lt;512[]&gt; but was:&lt;512[fed_]&gt;
at junit.framework.Assert.assertEquals(Assert.java:85)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
</failure>
  </testcase>
  </testsuite>

1 个答案:

答案 0 :(得分:0)

好吧,我把这个想法如下,为了别人的利益而张贴 -

            //Get a list of test cases with below code -
            testCaseList = docElement.getElementsByTagName(TEST_CASE_TAG);

            //Iterate through the list and set each item to testElement

            // Get Test Case status
            TestStatus testCaseStatus = TestStatus.FAILED;
            if(testElement.hasChildNodes()) {

                //Check if test failed ?
                elems = getDirectChildsByTagName(testElement, TEST_FAILURE_TAG);
                if(elems != null && elems.size()>0) {
                    logger.info("failed Tests :"+elems.size());
                    testCaseStatus = TtuTestStatus.FAILED;
                }
                //Check if test skipped ?
                else {
                    elems = getDirectChildsByTagName(testElement, TEST_SKIPPED_TAG);
                    if(elems != null && elems.size()>0) {
                        logger.info("Skipped Tests :"+elems.size());
                        testCaseStatus = TtuTestStatus.SKIPPED;
                    }
                }
            }
            else {
                testCaseStatus = TtuTestStatus.PASSED;
            }