使用jacoco时没有被teamcity捕获的junit测试失败

时间:2013-02-22 16:53:17

标签: ant junit teamcity jacoco

我试图通过使用jacoco,ant和teamcity将代码覆盖整合到我的项目中。但是,我意识到当jacoco任务围绕junit任务时,teamcity没有捕获失败的测试,即使测试失败,一切都很成功。

以下是我使用和不使用jacoco进行测试的2个测试任务,并查看teamcity bahaviours。

1-启用jacoco

<target name="-test">
    <echo message="JaCoCo activated"/>
    <!-- Import the JaCoCo Ant Task -->
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"/>
    <!-- Run your unit tests, adding the JaCoCo agent -->
    <jacoco:coverage destfile="${bin}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
        <junit fork="yes" printsummary="yes" haltonfailure="no" showoutput="false" failureProperty="test.failed" errorProperty="test.failed">
            <classpath>
                <path location="${lib}/${projectName}.jar"/>
                <path refid="project.classpath"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest todir="${reportingHome}">
                <fileset dir="${test}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    </jacoco:coverage>
    <copy todir="${completeReportDir}" overwrite="true">
        <fileset dir="${reportingHome}">
            <include name="*.xml"/>
        </fileset>
    </copy>
</target>

2-没有jacoco

<target name="-test">
    <echo message="JaCoCo activated"/>
    <!-- Import the JaCoCo Ant Task -->
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"/>
    <!-- Run your unit tests, adding the JaCoCo agent -->
    <!--<jacoco:coverage destfile="${bin}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">-->
        <junit fork="yes" printsummary="yes" haltonfailure="no" showoutput="false" failureProperty="test.failed" errorProperty="test.failed">
            <classpath>
                <path location="${lib}/${projectName}.jar"/>
                <path refid="project.classpath"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest todir="${reportingHome}">
                <fileset dir="${test}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
    <!--</jacoco:coverage>-->
    <copy todir="${completeReportDir}" overwrite="true">
        <fileset dir="${reportingHome}">
            <include name="*.xml"/>
        </fileset>
    </copy>
</target>

在2个测试版本之间只评论了jacoco任务。 Teamcity输出

[CommonBuildTasks.-test] echo
[08:26:21]: [echo] JaCoCo activated
[08:26:21]: [CommonBuildTasks.-test] jacoco:coverage (4s)
[08:26:21]: [jacoco:coverage] Enhancing junit with coverage.
[08:26:22]: [jacoco:coverage] Running ca.thalesgroup.socialnetworkanalysisorchestrator.impl.client.SocialNetworkAnalysisOrchestratorServiceProviderTest
[08:26:25]: [jacoco:coverage] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 3.511 sec
[08:26:26]: [jacoco:coverage] Test ca.thalesgroup.socialnetworkanalysisorchestrator.impl.client.SocialNetworkAnalysisOrchestratorServiceProviderTest FAILED
[08:26:26]: [CommonBuildTasks.-test] copy
[08:26:26]: [copy] Copying 1 file to C:\TeamCity\buildAgent\work\cc10e09e43249f57\reports

正如您所看到的,测试失败但是teamcity报告了成功构建。

我知道为什么会出现这种行为? 感谢

2 个答案:

答案 0 :(得分:1)

答案隐藏在您对JUnit-Task的调用中:

<junit haltonfailure="no">...</junit>

使用此配置,JUnit任务不会使失败测试的构建失败。这应该导致期望的行为:

<junit haltonfailure="yes">...</junit>

有关JUnit任务的配置,请参阅Ant文档。

答案 1 :(得分:1)

我通过使用代理任务而不是覆盖任务解决了这个问题。所以,而不是

<jacoco:coverage destfile="${bin}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">

使用:

<jacoco:agent property="agentvmparam" destfile="${bin}/jacoco.exec"/>
<junit fork="yes"...
    <jvmarg value="${agentvmparam}"/>
</junit>

代理任务使用与coverage任务相同的属性。然后,您可以启动junit任务,而无需将其包装在coverage任务中。这样,teamcity就能拦截junit任务输出。