【问题标题】:Missing Unit Test Coverage but have Unit Test Statistics缺少单元测试覆盖但有单元测试统计
【发布时间】:2014-02-21 22:50:55
【问题描述】:

我有一个 Jenkins Sonar 设置,带有一个 Maven 项目。这个 maven 项目只有一个 pom,但包含 java、javascript、html 等。所以对于声纳来说,它是一个多模块项目,这样我就可以得到每个部分的统计信息。

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

所以我将我的 jenkins 作业设置为执行 clean package 作为第一步,然后我们运行我们添加 Invoke Standalone Sonar Analysis 作为第二步。我们在项目的根目录中有一个 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 作业运行时,一切似乎都运行良好,但结果缺少单元测试覆盖率数字:

查看我发现的构建日志:

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,并尝试了那里的建议,但无济于事。所以我假设我错过了另一个设置或设置错误。

【问题讨论】:

    标签: jenkins sonarqube jacoco


    【解决方案1】:

    我错过了一个名为sonar.binaries 的属性。所以我添加了这一行

    project-java.sonar.binaries=target/classes
    

    到属性文件,然后就成功了。

    【讨论】:

      【解决方案2】:

      你能试试mvn clean install吗?

      【讨论】:

      • 我们大概是在同一时间发帖的。无论如何,事实证明我错过了告诉 jacoco 编译的类文件的位置所需的属性。不知何故错过了这一点,或者在我之前经历的所有示例中都假设它是知识。不过感谢您的建议。
      【解决方案3】:

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

      但在我的项目中,添加就足够了 org.jacoco:jacoco-maven-plugin:prepare-agent 到我的 maven 构建。

      我不需要任何声纳和 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 声纳插件 2.1

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多