3

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

这是我的 2 个测试任务,可以在有无 jacoco 的情况下进行测试,并查看 teamcity 的行为。

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 报告构建成功。

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

4

2 回答 2

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>

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

于 2013-04-17T16:58:48.133 回答
1

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

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

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

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

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

于 2013-02-22T16:58:37.483 回答