java - 如何解析JUnit结果xml文件并在java代码中获取测试用例?

标签 java xml junit ant junit5

Test_XyzDymanic类、方法testXyzDymanicallly和自定义Suite.xml中使用@TestFactory实现了dymanic JUnit5测试,以添加在运行时解析的多个测试用例。

根据 Suite.xml 中的情况,正在获取两个 map ma​​pExpectedma​​pAcutal,将其与 assertIterableEquals(mapExpected.entrySet(), mapAcutal.entrySet()) 进行比较

使用 Ant 构建调用 Junit,并将输出保存在 result.xml 中。

执行后,一些测试用例失败(当然),Junit 仅在控制台上打印一行,这不足以了解原因。 因此,使用 ReportGenerator 来生成基于 slimier 参数的 excel 文件,如 junit 的测试用例中所示。

查询: 对于失败的测试用例想要运行ReportGenerator,但不知道如何在Java代码中从result.xml读取失败的测试用例。 Junit是否提供任何类和解析方法来读取预定义PoJo中的result.xml?或者我们可以从 ant 读取并作为参数发送吗?

Ant 目标:

<target name="test-dynamic" depends="test-compile" description="Run JUnit tests">
    <junit printsummary="true" haltonerror="no" haltonfailure="no" dir="${test.bin}" fork="true">
        <sysproperty key="suite" value="${suite.xml}" />
        <classpath refid="junit5.jars" />

        <test name="Test_XyzDymanic" outfile="${result}" failureproperty="test.failed">
            <formatter type="xml" />
            <formatter usefile="false" type="brief" />
        </test>
    </junit>
</target>

<target name="fail-report">
    <echo message="One or more test(s) failed. Generating faliur analysis report..." />
    <java classname="report.ReportGenerator" failonerror="no"/>
</target>

结果.xml:

    ...
    <property name="sun.arch.data.model" value="32" />
  </properties>
  <testcase classname=")" name="Case: default case(testXyzDymanicallly" time="10.245">
    <failure message="iterable contents differ at index [75], expected: &lt;PropX.ShortName=NameXyz&gt; but was: &lt;PropX.ShortName=NameAbc&gt;" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: iterable contents differ at index [75], expected: &lt;PropX.ShortName=NameXyz&gt; but was: &lt;PropX.ShortName=NameAbc&gt;
    at Test_XyzDynamic.lambda$null$2(Test_XyzDynamic.java:41)
    at java.util.Optional.ifPresent(Optional.java:159)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
</failure>
  </testcase>

报告生成器

class ReportGenerator{
    main(String args){}
    generate(){}
  }

尝试过:

结果.xml:

<property name="sun.arch.data.model" value="32" />
  </properties>
  <testcase classname=")" name="testcase1" time="9.596" />
  <testcase classname=")" name="testcase2" time="5.702">
    <failure message="msg1" type="type1">
    desc 11111
</failure>
  </testcase>
  <testcase classname=")" name="testcase3" time="5.656">
    <failure message="msg2" type="type2">
    desc 22222
</failure>
  </testcase>
  <testcase classname="junit.framework.JUnit4TestCaseFacade" name="JUnit Vintage" time="0.003" />

Ant :

<xmlproperty file="result.xml" keeproot="false" />
<target name="main">
    <echo>1. ${testcase}</echo>
    <echo>2. ${testcase(name)}</echo>
    <echo>3. ${testcase.failure}</echo>
    <echo>4. ${testcase.failure(type)}</echo>
    <echo>5. ${testcase.failure(message)}</echo>
</target>

输出:

main:
     [echo] 1. ,
     [echo] 2. testcase1,testcase2,testcase3,JUnit Vintage
     [echo] 3. desc 11111,desc 22222
     [echo] 4. type1,type2
     [echo] 5. msg1,msg2

查询: 在控制台输出中,echo 2 有 testcase1,testcase2,testcase3,JUnit Vintage 但我想要 testcase2,testcase3 因为只有这两个测试用例根据 result.xml 具有失败标记。

最佳答案

无需在 ant 中读取 xml,而是将文件作为参数发送到主类并像任何其他 xml 一样解析文件并处理所需的值。

关于java - 如何解析JUnit结果xml文件并在java代码中获取测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55624209/

相关文章:

php - 使用 PHP 遍历来自 Yandex API 的 XML 响应

Android XML 架构验证

junit - Mockito NotAMockException 当它显然是

java - Maven testCompile 适用于 Java 9 Build 166,但适用于 Build 167 失败

JAVA jframe.remove(panel) 无法按预期工作

java - 字符串实习生的行为?

sql - 在 SQL 中使用 FOR XML 为同一属性选择多个值

java - 注释方法 arg @Nonnull 时 JUnit 测试失败

java - 仅分析 Java 应用程序的某些部分/类

java - hazelcast 的非 Spring 实现中缓存值的替代方案是什么?