2

我对蚂蚁声纳任务有疑问。此任务以成功结束,但不运行单元测试并且不显示代码覆盖率。

测试任务

 <target name="test" depends=".....">
    <path id="classpath">
        <fileset dir="test/lib" includes="**/*.jar"/>
        <fileset dir="lib" includes="**/*.jar"/>
        <pathelement location="......."/>
    </path>

    <mkdir dir="build/tests"/>
    <javac srcdir="test/src" destdir="build/tests" includes="**/*.java" debug="${debug}" deprecation="${deprecation}" optimize="${optimize}" nowarn="${nowarn}" fork="true">
        <classpath refid="classpath"/>
    </javac>
    <copy todir="build/tests">
        <fileset dir="test/src" excludes="**/*.java"/>
    </copy>

<jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
     <junit printsummary="yes" fork="true" haltonfailure="false" showoutput="true" failureproperty="test.failed">
            <formatter type="xml"/>
         <classpath refid="classpath"/>
            <classpath>
                <pathelement location="build/tests"/>
         </classpath>
            <test name="com........MainTestSuite" todir="build"/>
     </junit>
</jacoco:coverage>
    <fail message="Test failure detected, check test results." if="test.failed"/>
</target>

和声纳任务:

<target name="sonar" depends="build">
    <property name="sonar.tests" value="test" />
    <property name="sonar.libraries" value="" />
    <property name="sonar.surefire.reportsPath" value="sonarWorkDir" />

    <!-- The following properties are required to use JaCoCo: -->
    <!-- 1. Tells Sonar to run the unit tests -->
    <property name="sonar.dynamicAnalysis" value="true" />
    <!-- 2. Tells Sonar which "tests" targets to run -->
    <property name="sonar.jacoco.antTargets" value="test" />
    <!-- 3. Tells Sonar to use JaCoCo as the code coverage engine -->
    <property name="sonar.core.codeCoveragePlugin" value="jacoco" />

    <!-- Execute Sonar -->
    <sonar:sonar key="${JOB_NAME}" version="${VERSION}" xmlns:sonar="antlib:org.sonar.ant">
        <sources>
            <path location="...../src" />
        </sources>

         <binaries>
            <path location="build/....." />
        </binaries>

    </sonar:sonar>
</target>

在声纳任务运行之前,我收到警告 10:00:20.290 WARN ospjJaCoCoPlugin - 未收集覆盖信息。也许您忘记在编译的类中包含调试信息?

4

2 回答 2

1

我建议你:

于 2013-01-08T09:38:22.470 回答
0

有点晚了,但我最近才遇到这个问题,在互联网上找不到简单的解决方案。相反,为了解决它,我只是听取了堆栈跟踪的建议。Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?

然后回到我的 ant 文件并确保源文件(不是测试)正在调试模式下编译。显然,jacoco 插件需要额外的信息(如行号)来计算代码覆盖率。更改 ant 文件后,您最终应该会在声纳中看到代码覆盖率。

<javac srcdir="${source.dir}" target="1.6" source="1.6" destdir="${build.classes.dir}" debug="on">
    ...
</javac>

此外,请确保包含以下声纳属性:

sonar.binaries=build/classes
sonar.tests=junit/tests
sonar.dynamicAnalysis=reuseReports
sonar.junit.reportsPath=build/test-reports
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=build/test-reports/jacoco.exec

因此,您可能想要删除sonar.antTargets并将sonar.dynamicAnalysis' 值更改为reuseReports

于 2014-02-25T18:17:07.940 回答