缺少单元测试覆盖率但具有单元测试统计信息

时间:2014-01-29 21:19:32

标签: jenkins sonarqube jacoco

我有一个詹金斯声纳设置,有一个maven项目。这个maven项目只有一个pom,但是包含java,javascript,html等。因此对于声纳它是一个多模块项目,这样我就可以得到每个部分的统计数据。

我们还希望获得项目的代码覆盖率分析,这意味着我们需要在声纳分析之前运行测试并导出它们(至少从我收集到的声纳中,声纳不能做这种类型的分析)。

所以我将jenkins工作设置为clean package作为第一步,然后我们运行我们添加调用独立声纳分析作为第二步。我们在项目的根目录中有一个 sonar-project.properties 文件,其中包含所有各种设置。这是:

sonar.projectKey=Project
sonar.projectName=Project
sonar.projectVersion=0.2.0-SNAPSHOT
sonar.projectDescription=Super Project
sonar.modules=project-java,project-web,project-js
project-java.sonar.dynamicAnalysis=reuseReports
project-java.sonar.core.codeCoveragePlugin=jacoco
project-java.sonar.dynamicAnalysis=reuseReports
project-java.sonar.junit.reportsPath=target/surefire-reports
project-java.sonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec
project-java.sonar.projectName=project-java
project-java.sonar.language=java
project-java.sonar.projectBaseDir=.
project-java.sonar.sources=src/main/java
project-java.sonar.java.source=1.7
project-web.sonar.projectName=project-web
project-web.sonar.language=web
project-web.sonar.projectBaseDir=.
project-web.sonar.sources=src/main/webapp/partials
project-js.sonar.projectName=project-js
project-js.sonar.language=js
project-js.sonar.projectBaseDir=.
project-js.sonar.sources=src/main/webapp/js

现在在我的pom中我添加了以下内容(大部分时间都来自Creating Code Coverage Reports for Unit And Integration Tests with The JaCoCo Maven Plugin):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <!-- Sets the VM argument line used when unit tests are run. -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <!--<skipTests>${skip.unit.tests}</skipTests>-->
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>   
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.4.201312101107</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
         <execution>
             <id>pre-unit-test</id>
             <goals>
                 <goal>prepare-agent</goal>
             </goals>
             <configuration>
                 <!-- Sets the path to the file which contains the execution data. -->
                 <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                 <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                 <propertyName>surefireArgLine</propertyName>
             </configuration>
         </execution>
         <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
         <execution>
             <id>post-unit-test</id>
             <phase>test</phase>
             <goals>
                 <goal>report</goal>
             </goals>
             <configuration>
                 <!-- Sets the path to the file which contains the execution data. -->
                 <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                 <!-- Sets the output directory for the code coverage report. -->
                 <outputDirectory>${project.build.directory}/jacoco-ut</outputDirectory>
             </configuration>
         </execution>
    </executions>
</plugin>

所以,当我mvn clean package本地运行时,我可以转到目标目录,我看到生成的报告(我知道声纳不使用这些,但它有代码覆盖率信息) )。现在,当Sonar作业运行时,一切似乎都运行正常,但结果却缺少单元测试覆盖率数字:

Sample Output

查看构建日志,我发现:

14:39:43.929 INFO  - Sensor JaCoCoSensor...
14:39:43.932 INFO  - Project coverage is set to 0% since there is no directories with classes.
14:39:43.932 INFO  - Sensor JaCoCoSensor done: 3 ms

我见过“Project coverage is set to 0%” – JaCoCo and Sonar in Jenkins,并尝试了那里的建议无济于事。所以我假设我错过了另一个设置或者出了问题。

3 个答案:

答案 0 :(得分:3)

我错过了一个名为sonar.binaries的财产。所以我添加了一行

project-java.sonar.binaries=target/classes

到属性文件,然后就可以了。

答案 1 :(得分:1)

您可以尝试 mvn clean install 吗?

答案 2 :(得分:0)

我遇到了同样的问题:缺少报道。

但是在我的项目中添加就足够了 org.jacoco:jacoco-maven-plugin:prepare-agent to my maven build。

我不需要任何声纳和jacoco配置(声纳与jenkins在同一台机器上运行)1只需致电:

clean org.jacoco:jacoco-maven-plugin:prepare-agent install -P integration-tests --fail-at-end

之后通过jenkins声纳插件通过后置构建动作调用声纳。 http://docs.codehaus.org/display/SONAR/Configuring+SonarQube+Jenkins+Plugin

版本:

詹金斯1.583 Sonar Plugin 2.1

PS 我在pom中配置了插件版本。