我的项目正在使用 Ant,它有几个测试套件。由于每个套件都以类似的方式运行,因此定义了一个宏:
<macrodef name="exec-tests">
<attribute name="test-suite" />
<element name="test-run" implicit="yes" />
<sequential>
<junit printsummary="yes" haltonfailure="true" haltonerror="true" showoutput="true" outputtoformatters="true" fork="true" maxmemory="512m">
<jvmarg value="-XX:MaxPermSize=256m" />
<jvmarg value="-Xmx512m" />
<jvmarg value="-Xms512m" />
<classpath refid="test.run.class.path" />
<formatter type="plain" usefile="false" />
<formatter type="xml" usefile="true" />
<test name="@{test-suite}" todir="${test.build.results.dir}" />
</junit>
</sequential>
</macrodef>
所以有几个目标运行不同的套件,如下所示:
<target name="run-xxx-tests" depends="build-tests">
<exec-tests test-suite="com.mycompany.XxxTestsSuite" />
</target>
<target name="run-yyy-tests" depends="build-tests">
<exec-tests test-suite="com.mycompany.YyyTestsSuite" />
</target>
现在我还想运行一个带有 Jacoco 覆盖率的测试套件。所以这样做会很好:
<target name="run-xxx-tests-with-coverage" depends="build-tests">
<jacoco:coverage destfile="${test.coverage.unit.file}">
<exec-tests test-suite="com.mycompany.XxxTestsSuite" />
</jacoco:coverage>
</target>
但是,Jacoco 似乎不支持覆盖标签中的宏,因为我遇到了错误:
Caused by: C:\Users\taavi\projects\cds\build.xml:87: exec-tests is not a valid child of the coverage task
at org.jacoco.ant.CoverageTask.addTask(CoverageTask.java:68)
at org.apache.tools.ant.UnknownElement.handleChildren(UnknownElement.java:367)
现在我创建了另一个与“exec-tests”非常相似的宏定义,但只是增加了覆盖范围。这并不重要,但我想知道有没有办法仍然避免这个重复的“junit”任务部分?
k6ps