在Test_XyzDymanic类、方法testXyzDymanicallly和自定义Suite.xml中使用@TestFactory实现了dymanic JUnit5测试,以添加在运行时解析的多个测试用例。
根据 Suite.xml 中的情况,正在获取两个 map mapExpected 和 mapAcutal,将其与 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: <PropX.ShortName=NameXyz> but was: <PropX.ShortName=NameAbc>" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: iterable contents differ at index [75], expected: <PropX.ShortName=NameXyz> but was: <PropX.ShortName=NameAbc>
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/