Ant,Sonar - 0%代码覆盖率

时间:2013-01-08 09:13:30

标签: ant sonarqube

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

测试任务

 <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 o.s.p.j.JaCoCoPlugin - 未收集覆盖范围信息。也许您忘记将调试信息包含在已编译的类中?

2 个答案:

答案 0 :(得分:1)

我建议你:

答案 1 :(得分: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