2015-10-20 30 views
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

回答

2

<jacoco:coverage>任務有an enabled attribute可能有用...

If set to true coverage data will be collected for the contained task.

要使用enabled,你可以做一些修改<exec-tests>

  • 移動<jacoco:coverage>進去
  • 添加可選coverage.destfile屬性

會如何看待?

<macrodef name="exec-tests"> 
    <attribute name="test-suite" /> 
    <!-- If <test-suite> is called without coverage.destfile, then --> 
    <!-- coverage.enabled won't be set to true and coverage info won't --> 
    <!-- be collected. --> 
    <attribute name="coverage.destfile" default="" /> 
    <element name="test-run" implicit="yes" /> 
    <sequential> 
     <local name="coverage.enabled" /> 
     <condition property="coverage.enabled" value="false" else="true"> 
      <equals arg1="@{coverage.destfile}" arg2="" /> 
     </condition> 
     <jacoco:coverage enabled="${coverage.enabled}" destfile="@{coverage.destfile}"> 
      <junit ...> 
      ... 
      </junit> 
     </jacoco:coverage> 
    </sequential> 
</macrodef> 

然後,如果覆蓋信息應收集每個測試可以指定...

<exec-tests 
    test-suite="com.mycompany.XxxTestsSuite" 
    coverage.destfile="${test.coverage.unit.file}" /> 

在上面的例子中,覆蓋信息將被收集,因爲提供了coverage.destfile

+0

是的,這個想法對我有用。我甚至不需要本地標籤,我只是向macrodef引入了覆蓋啓用屬性:'' – k6ps