SonarQube。无法显示代码覆盖率

时间:2017-09-26 05:51:21

标签: java unit-testing sonarqube integration-testing code-coverage

我想设置我的声纳(版本6.1)确实显示我的项目代码覆盖率。我设置了JaCoCo,可以使用命令

在我的计算机上本地生成CodeCoverage
  

mvn clean package

但是我无法将报道上传到Sonar。为此,我使用以下命令:

  

mvn -Dsonar.login =< my_login> -Dsonar.password = LT; my_pwd>   -Dsonar.host.url = LT;声纳URL> -Dsonar.projectKey = LT; productKey>清洁包声纳:声纳-Psonar -Ptomcat

这是pom.xml中的JaCoco设置

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
  <configuration>
    <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
    <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
  </configuration>
  <executions>
    <execution>
      <id>jacoco-initialize</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>jacoco-site</id>
      <phase>package</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

我看到以下输出:

[INFO] -------------  Scan risk
[INFO] Language is forced to java
[INFO] Base dir: F:\repo\orchestrator
[INFO] Working dir: F:\repo\orchestrator\target\sonar
[INFO] Source encoding: UTF-8, default locale: ru_RU
[INFO] Quality profile for java: Sonar way
[INFO] Sensor Lines Sensor
[INFO] Sensor Lines Sensor (done) | time=0ms
[INFO] Sensor SCM Sensor
[INFO] Sensor SCM Sensor (done) | time=0ms
[INFO] Sensor Embedded CSS Analyzer Sensor
[INFO] 0 source files to be analyzed
[INFO] Sensor Embedded CSS Analyzer Sensor (done) | time=1ms
[INFO] Sensor Coverage Report Import
[INFO] Sensor Coverage Report Import (done) | time=0ms
[INFO] Sensor Coverage Report Import
[INFO] Sensor Coverage Report Import (done) | time=0ms
[INFO] Sensor Unit Test Results Import
[INFO] Sensor Unit Test Results Import (done) | time=0ms
[INFO] Sensor XmlFileSensor
[INFO] Sensor XmlFileSensor (done) | time=0ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=0ms
[INFO] Sensor Code Colorizer Sensor
[INFO] Sensor Code Colorizer Sensor (done) | time=0ms
[INFO] Sensor CPD Block Indexer
[INFO] JavaCpdBlockIndexer is used for java
[INFO] 0/0 source files have been analyzed
[INFO] Sensor CPD Block Indexer (done) | time=0ms
[INFO] Calculating CPD for 592 files
[INFO] CPD calculation finished
[INFO] Analysis report generated in 20712ms, dir size=5 MB
[INFO] Analysis reports compressed in 2506ms, zip size=3 MB
[INFO] Analysis report uploaded in 2704ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://URL/dashboard/index/<ProductId>:parent
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://URL/api/ce/task?id=AV68su2eddnYVrlQ9oP-
[INFO] Task total time: 1:36.284 s

很高兴收到有关代码覆盖率发布到Sonar的更多信息。任何建议/提示将受到高度赞赏。 感谢名单!

1 个答案:

答案 0 :(得分:2)

这里可能出错的一些事情。首先看起来很可疑的是Sonar报告要分析的0个文件。它不会上传未分析的文件的覆盖范围。

这可能是由您错误定义的个人资料造成的。 mvn -P选项接受以逗号分隔的列表。您的第二个-Ptomcat可能会覆盖第一个-Psonar。更好地使用-Psonar,tomcat

还要确保在项目根目录中运行命令(pom.xml应该在哪里)并且您的Sonar服务器已安装并正确配置了Java和Coverage插件。例如,服务器上的代码覆盖率设置不得排除java文件。

我还使用validate代替package,因此您的集成测试(failsafe插件)也会运行。

吹毛求疵:

如果您使用的是Maven,则不需要

-Dsonar.projectKey=< productKey >,除非您想覆盖pom.xml中的默认项目名称。