正确设置SonarQube以覆盖代码

我正在使用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命令行来运行更新项目。

声纳云上的Overview板看起来像这样:

正确设置SonarQube以覆盖代码

我至少可以识别单元测试,但是就代码覆盖率而言,我仍然处于0%的水平。此外,这是Measures面板:

正确设置SonarQube以覆盖代码

显然,我的测试没有涉及任何行。现在,我知道这意味着我很可能没有正确地获得测试结果,但是我不确定该怎么做。

让我感到困惑的是,尽管Sonarqube识别了我的测试,但实际上它说测试本身的代码行没有被测试。这应该是什么意思?

正确设置SonarQube以覆盖代码

duanchongxixi 回答:正确设置SonarQube以覆盖代码

从SonarQube的documentation

  

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

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

SonarQube提供this guide来创建和导入Jacoco的报告。

,

这是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
,

我们有同样的问题。您需要在pom.xml中添加coverage块 可以参考以下示例pom.xml

4.0.0 org.springframework.boot 弹簧启动启动器父母 2.3.3发行 测验 测试应用 0.0.1-快照 测试应用 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

本文链接:https://www.f2er.com/3044372.html

大家都在问