1
我的項目使用Ant,它有幾個測試套件。由於每間套房都以類似的方式運行,存在定義一個宏:從Jacoco的覆蓋任務中運行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)
現在我創造了另一個macrodef即非常類似於「執行測試」,但只是增加了覆蓋面。這並不重要,但我想知道還有什麼方法可以避免這種重複的「junit」任務部分?
k6ps
是的,這個想法對我有用。我甚至不需要本地標籤,我只是向macrodef引入了覆蓋啓用屬性:'' –
k6ps