Spring Boot SonarCloud覆盖率在 Spring 始终为0%,Gradle项目在bitbucket管道上

beq87vna  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(257)

我目前正尝试在我的SpringBoot项目中配置一个带有gradle和bitbucket-pipelines的Sonarcloud,但是对于每一个PR和每一个分支,它总是显示0%的行覆盖率。
我已经将插件配置添加到我的build.gradle文件中:

id "org.sonarqube" version "3.4.0.2513"

这是我的bitbucket-pipelines.yml文件

image: openjdk:11

clone:
  depth: full              # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - gradle
          - sonar
        script:
          - ./gradlew build sonarqube
        artifacts:
          - build/libs/**

pipelines:                 # More info here: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html
  branches:
    master:
      - step: *build-test-sonarcloud
  pull-requests:
    '**':
      - step: *build-test-sonarcloud

Bitbucket上的一切似乎都配置正确,管道为每个PR和提交运行,但由于覆盖率为0%,所有这些都失败了。我希望Sonar和Bitbucket PR装饰上显示正确的测试覆盖率。是否缺少任何配置?

dzjeubhm

dzjeubhm1#

虽然SonarQube支持测试覆盖率报告,但它本身并不生成测试覆盖率报告。您必须使用第三方工具生成测试覆盖率报告,然后配置SonarQube以将第三方工具的结果考虑在内。
从他们的文档中:
SonarQube支持将测试覆盖率报告作为Java项目分析的一部分。但是,SonarQube本身并不生成覆盖率报告。相反,您必须设置一个第三方工具来生成报告,作为构建过程的一部分。
在第三方工具中,SonarQube直接支持JaCoCo,因此,由于您使用的是gradle,您只需在build.gradle中应用该插件:

plugins {
    id "jacoco" // <-- add here
    id "org.sonarqube" version "3.4.0.2513"
}

然后配置JaCoCo任务:

jacocoTestReport {
    reports {
        xml.enabled true
    }
}

根据SonarQube文档,它们检测JaCoCo自动存储覆盖率报告的默认位置,因此不需要进一步配置。
请注意,您还可以使用其他覆盖率工具,您并不局限于JaCoCo,但它应该是实现起来最简单的工具。
有关Java测试覆盖率的文档可在以下位置找到:https://docs.sonarqube.org/latest/analysis/test-coverage/java-test-coverage/

相关问题