为代码覆盖率正确设置SonarQube

14

我正在使用Maven在SpringBoot后端应用服务器上使用JUnit5。这是位于项目根目录下的sonar-project.properties文件:

Translated:

我在使用 Maven 在 SpringBoot 后端应用服务器上使用 JUnit5。这是项目根目录下的 sonar-project.properties 文件:

sonar.host.url=https://sonarcloud.io
sonar.login=xxx
sonar.organization=xxx
sonar.projectKey=xxx

sonar.sourceEncoding=UTF-8
sonar.language=java
sonar.java.source=12

sonar.sources=src/main/java
sonar.test=src/test
sonar.java.binaries=target/classes
sonar.junit.reportPaths=target/test-results/TEST-**.xml

我使用sonar-scanner命令行在构建/测试后运行更新项目。

在SonarCloud上,Overview面板如下:

overview

我至少让单元测试被识别出来了,但是某种程度上我仍然没有代码覆盖率(0%)。此外,这是Measures面板:

measures

显然,我的测试根本没有覆盖任何行。现在,我知道这意味着我很可能没有正确连接测试结果,但我不确定该怎么做。

让我困惑的另一件事是,尽管SonarQube识别了我的测试,它实际上说测试本身的代码行没有被测试。这是什么意思?

Testing tests?


你在安装Sonarqube之后是否运行了测试?代码覆盖率是从运行数据中获取的。你还需要设置覆盖变量。 https://docs.sonarqube.org/latest/analysis/coverage/ - Michele Dorigatti
这个项目已经安装了Sonarqube一周了。但是我刚刚开始添加单元测试,因此我没有意识到它的某个部分没有正常工作。哪个变量是“覆盖率变量”? - payne
SonarSource分析器不运行您的测试或生成报告。它们只导入预先生成的报告。下面是导入覆盖率和执行报告的语言和工具特定的分析参数。您需要使用jacoco或类似的工具。 - Michele Dorigatti
是的,我知道这个。使用 mvn test 会生成一个 surefire 报告:这就是我想要插入的东西,对吧?只是很难知道应该确定哪个变量集合。 - payne
1
我认为这还不够。你需要使用像jacoco这样的代码覆盖率工具。 - Michele Dorigatti
啊!我现在明白了。surefire 只用于测试的 执行。不知何故,它会生成一堆 XML 报告,但似乎这些报告不能被 SonarQube 解释。我得研究一下 jacoco。谢谢! - payne
3个回答

21

来自SonarQube的文档

SonarSource分析器不会运行您的测试或生成报告。它们只导入预先生成的报告。

用于为Java生成代码覆盖率的流行库是Jacoco

SonarQube提供了此指南来创建和导入Jacoco的报告。


1
我不知怎么忽略了“执行”和“覆盖率”是两个不同的事情。你的回答让我意识到了我的错误。谢谢!我会适当地标记它。最终,我会编辑一个可用的sonar-project.properties文件。 - payne
SonarQube是一个非常好用的工具,但是需要很多组件才能使其正常运行。 - Michele Dorigatti

6

这是可工作的sonar-project.properties文件:

# SONAR CLOUD CONFIGS
sonar.host.url=https://sonarcloud.io
sonar.organization=xxx
sonar.projectKey=xxx
sonar.login=xxx

# SOURCES
sonar.java.source=12
sonar.sources=src/main/java
sonar.java.binaries=target/classes
sonar.sourceEncoding=UTF-8

# EXCLUSIONS
# (exclusion of Lombok-generated stuff comes from the `lombok.config` file)
sonar.coverage.exclusions = **/*Exception.java , **/MySpringBootApplication.java

# TESTS
sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
sonar.junit.reportsPath=target/surefire-reports/TEST-*.xml
sonar.tests=src/test/java

3
我们遇到了同样的问题。您需要在pom.xml中添加覆盖块,可以参考以下示例pom.xml:

4.0.0

org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE
com.test testapp 0.0.1-SNAPSHOT testapp Spring Boot演示项目

<properties>
    <java.version>1.8</java.version>
    <sonar.coverage.jacoco.xmlReportPaths>../target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
      <groupId>org.sonarsource.scanner.maven</groupId>
      <artifactId>sonar-maven-plugin</artifactId>
      <version>3.6.0.1398</version>
    </plugin>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
    </plugin>
    </plugins>
</build>

<profiles>
<profile>
  <id>coverage</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>

参考链接:https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven/maven-basic


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接