从Jacoco的覆盖任务中运行Ant宏

时间:2015-10-20 08:36:12

标签: ant jacoco macrodef

我的项目正在使用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)

现在我创建了另一个非常类似于&#34; exec-tests&#34;的宏定义。但只是增加了报道。这并不重要,但我想知道是否还有任何方法可以避免这种重复&#34; junit&#34;任务部分?

k6ps

1 个答案:

答案 0 :(得分:2)

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

  

如果设置为true,将为所包含的任务收集覆盖率数据。

要使用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