SonarQube 不显示通过 Gradle 完全覆盖的类的每个文件的详细报告

2022-01-17 00:00:00 sonarqube intellij-idea gradle java jacoco

我正在 IntelliJ Java IDE 中运行 Gradle 构建.)?p>

这是我的 build.gradle:

存储库{mavenCentral()}应用插件:'java'应用插件:'jacoco'应用插件:声纳亚军"源兼容性 = 1.7项目版本 = '1.0'project.group = 'com.acme.sandbox'project.description = '只是一个测试项目'依赖{testCompile 组:'junit',名称:'junit',版本:'4.11'}//JaCoCo 测试覆盖率配置tasks.withType(Test) { 任务 ->雅可{附加=假}}//声纳配置sonarRunner {声纳属性 {属性 'sonar.host.url', 'http://host:port'属性sonar.jdbc.url"、myJdbcURL"属性sonar.jdbc.username"、dbuser"属性sonar.jdbc.password",dbpass"属性'sonar.login','builduser'属性'sonar.password','buildpass'属性 'sonar.profile', 'profilename'属性'sonar.branch','DEV'属性'sonar.language','java'属性 'sonar.sourceEncoding', 'UTF-8'属性 'sonar.verbose', 'true'//属性 'sonar.tests', "$projectDir\src\test\java"//属性 'sonar.binaries', "${buildDir}\classes\main,${buildDir}\classes\test"//属性 'sonar.java.coveragePlugin', 'jacoco'属性 'sonar.jacoco.reportPath', "${buildDir}\jacoco\test.exec"属性 'sonar.junit.reportsPath', "${buildDir}\test-results"}}

现在,如果您执行 clean,然后执行 sonarRunner,则会创建并处理 JaCoCo test.exec 文件:

<snip>18:35:27.705 调试 - 初始化程序:18:35:27.706 信息 - 基本目录:D:path	oJaCoCoTest18:35:27.706 信息 - 工作目录:D:path	oJaCoCoTestuildsonar18:35:27.707 信息 - 源目录:D:path	oJaCoCoTestsrcmainjava18:35:27.708 信息 - 测试目录:D:path	oJaCoCoTestsrc	estjava18:35:27.708 信息 - 二进制目录:D:path	oJaCoCoTestuildclassesmain<剪辑>18:35:27.931 信息 - 未找到 JaCoCo IT 报告.18:35:27.933 信息 - JaCoCo 报告未找到.18:35:27.938 调试 - 传感器:JavaSquidSensor ->QProfileSensor ->FindbugsSensor ->CpdSensor ->PmdSensor ->SurefireSensor ->CheckstyleSensor ->InitialOpenIssuesSensor ->ProfileEventsSensor ->ProjectLinksSensor ->版本事件传感器 ->FileHashSensor ->JaCoCo传感器<剪辑>18:35:34.055 信息 - 传感器 SurefireSensor...18:35:34.056 信息 - 解析 D:path	oJaCoCoTestuild	est-results18:35:34.116 信息 - 传感器 SurefireSensor 完成:61 毫秒<剪辑>18:35:34.936 信息 - 传感器 JaCoCoSensor...18:35:34.939 信息 - 分析 D:path	oJaCoCoTestuildjacoco	est.exec18:35:35.027 信息 - 没有关于每次测试覆盖率的信息.18:35:35.028 信息 - 传感器 JaCoCoSensor 完成:92 毫秒

这篇文章 建议设置sonar.tests,我试过没有效果.日志文件显示检测到测试源和二进制文件位于正确的位置.

FWIW,我在 Maven 构建中使用了相同的设置并且它有效.但是这里不能使用 Maven.

版本号:

  • SonarQube 4.3
  • SonarQube Java 生态系统 2.2.1
  • Gradle 1.12(又名:1.11)
  • IntelliJ IDEA Ultimate 13.1.3
  • Java 1.7

这里是完整测试项目供下载,还有完整日志.

解决方案

这就是所谓的通缉"行为.Sonar 不显示覆盖率为 100% 的类.在您的项目中,您有一个类被测试完全覆盖,因此没有什么可显示的.

仅供参考:

即使您有 100% 覆盖的课程,您也可以通过在声纳搜索框(右上角)中搜索来查看该课程的覆盖率.

<小时>

如果您看到 .exec 并且它的大小很有价值,那么您已经收集了覆盖率数据.

查看我的常见问题解答

我为您的项目获得了这个代码分析小部件:

最后,在我添加了另一种被测试发现的方法之后,测试覆盖率下降到了 80%:

我得到了你需要的东西——按行覆盖:

顺便说一句,您为我的声纳修改的 build.gradle(这个 // 对我不起作用):

存储库{mavenCentral()}应用插件:'java'应用插件:'jacoco'应用插件:声纳亚军"源兼容性 = 1.7项目版本 = '1.0'project.group = 'com.acme.sandbox'project.description = '只是一个测试项目'依赖{testCompile 组:'junit',名称:'junit',版本:'4.11'}//JaCoCo 测试覆盖率配置tasks.withType(Test) { 任务 ->雅可{附加=假}}//声纳配置sonarRunner {声纳属性 {//关于 SonarQube 服务器的一般信息属性 'sonar.host.url', 'http://localhost:9000'属性 'sonar.jdbc.url'、'jdbc:h2:tcp://localhost:9092/sonar'属性sonar.jdbc.validationQuery",选择 1"属性sonar.jdbc.driverClassName",org.hibernate.dialect.H2Dialect"属性sonar.jdbc.password",声纳"属性sonar.jdbc.password",声纳"属性'sonar.login','jenkins'属性'sonar.password','jenkins'//这个项目的信息//属性 'sonar.profile', 'profilename'属性'sonar.branch','DEV'属性'sonar.language','java'属性 'sonar.sourceEncoding', 'UTF-8'属性 'sonar.verbose', 'true'//属性 'sonar.tests', "$projectDir\src\test\java"属性 'sonar.binaries', "${buildDir}/classes/main/"//在 SonarQube 运行之前执行 JaCoCo 以准备好报告文件//属性 'sonar.java.coveragePlugin', 'jacoco'属性 'sonar.jacoco.reportPath', "${buildDir}/jacoco/test.exec"属性 'sonar.junit.reportsPath', "${buildDir}/test-results"}}

I am running a Gradle build inside the IntelliJ Java IDE. The SonarQube runner Gradle plugin is used along with the JaCoCo Gradle plugin.

Problem: I am getting the message No information about coverage per test., (not a duplicate of this post, see below) and the coverage appears in SonarQube, but only as an overall percentage, not a detailed report per file:

Am I doing something wrong? Is it a bug in SonarQube maybe (as it was with Cobertura recently)?

Here is my build.gradle:

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'sonar-runner'

sourceCompatibility = 1.7
project.version = '1.0'
project.group = 'com.acme.sandbox'
project.description = 'just a test project'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

// JaCoCo test coverage configuration
tasks.withType(Test) { task ->
    jacoco {
        append = false
    }
}

// Sonar configuration
sonarRunner {
  sonarProperties {
    property 'sonar.host.url', 'http://host:port'
    property 'sonar.jdbc.url', 'myJdbcURL'
    property 'sonar.jdbc.username', 'dbuser'
    property 'sonar.jdbc.password', 'dbpass'
    property 'sonar.login', 'builduser'
    property 'sonar.password', 'buildpass'

    property 'sonar.profile', 'profilename'
    property 'sonar.branch', 'DEV'
    property 'sonar.language', 'java'
    property 'sonar.sourceEncoding', 'UTF-8'
    property 'sonar.verbose', 'true'
    //property 'sonar.tests', "$projectDir\src\test\java"
    //property 'sonar.binaries', "${buildDir}\classes\main,${buildDir}\classes\test"

    //property 'sonar.java.coveragePlugin', 'jacoco'
    property 'sonar.jacoco.reportPath', "${buildDir}\jacoco\test.exec"
    property 'sonar.junit.reportsPath', "${buildDir}\test-results"
  }
}

Now if you execute a clean, followed by sonarRunner, a JaCoCo test.exec file gets created and processed:

<snip>
18:35:27.705 DEBUG - Initializers : 
18:35:27.706 INFO  - Base dir: D:path	oJaCoCoTest
18:35:27.706 INFO  - Working dir: D:path	oJaCoCoTestuildsonar
18:35:27.707 INFO  - Source dirs: D:path	oJaCoCoTestsrcmainjava
18:35:27.708 INFO  - Test dirs: D:path	oJaCoCoTestsrc	estjava
18:35:27.708 INFO  - Binary dirs: D:path	oJaCoCoTestuildclassesmain
<snip>
18:35:27.931 INFO  - JaCoCo IT report not found.
18:35:27.933 INFO  - JaCoCo reports not found.
18:35:27.938 DEBUG - Sensors : JavaSquidSensor -> QProfileSensor -> FindbugsSensor -> CpdSensor -> PmdSensor -> SurefireSensor -> CheckstyleSensor -> InitialOpenIssuesSensor -> ProfileEventsSensor -> ProjectLinksSensor -> VersionEventsSensor -> FileHashSensor -> JaCoCoSensor
<snip>
18:35:34.055 INFO  - Sensor SurefireSensor...
18:35:34.056 INFO  - parsing D:path	oJaCoCoTestuild	est-results
18:35:34.116 INFO  - Sensor SurefireSensor done: 61 ms
<snip>
18:35:34.936 INFO  - Sensor JaCoCoSensor...
18:35:34.939 INFO  - Analysing D:path	oJaCoCoTestuildjacoco	est.exec
18:35:35.027 INFO  - No information about coverage per test.
18:35:35.028 INFO  - Sensor JaCoCoSensor done: 92 ms

This post suggests to set sonar.tests, which I've tried to no effect. The log file shows that test sources and binaries are detected to be at the correct locations.

FWIW, I used the same settings with a Maven build and it worked. Can't use Maven here though.

Version numbers:

  • SonarQube 4.3
  • SonarQube Java Ecosystem 2.2.1
  • Gradle 1.12 (also: 1.11)
  • IntelliJ IDEA Ultimate 13.1.3
  • Java 1.7

Here is the full test project for download, and also the full log.

解决方案

This is what is called 'wanted' behaviour. Sonar does not display classes with 100% coverage. In your project you have one class fully covered by tests, so nothing to show.

FYI:

Even if you have 100% covered class, you can look at this class coverage by searching in sonars search box (top-right corner).


If you see .exec and it is of valuable size, then you already collected coverage data.

See my FAQ

I got this code analysis widget for your project:

And finally after I added another method uncovered by tests and test coverage dropped to 80%:

I got what you need - coverage by line:

By the way, your modified build.gradle for my sonar (this // does not work for me):

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'sonar-runner'


sourceCompatibility = 1.7
project.version = '1.0'
project.group = 'com.acme.sandbox'
project.description = 'just a test project'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

// JaCoCo test coverage configuration
tasks.withType(Test) { task ->
    jacoco {
        append = false
    }
}

// Sonar configuration
sonarRunner {
    sonarProperties {
        // general information about the SonarQube server
        property 'sonar.host.url', 'http://localhost:9000'
        property 'sonar.jdbc.url', 'jdbc:h2:tcp://localhost:9092/sonar'
        property 'sonar.jdbc.validationQuery', 'select 1'
        property 'sonar.jdbc.driverClassName', 'org.hibernate.dialect.H2Dialect'
        property 'sonar.jdbc.password', 'sonar'
        property 'sonar.jdbc.password', 'sonar'
        property 'sonar.login', 'jenkins'
        property 'sonar.password', 'jenkins'

        // information about this project
        //property 'sonar.profile', 'profilename'
        property 'sonar.branch', 'DEV'
        property 'sonar.language', 'java'
        property 'sonar.sourceEncoding', 'UTF-8'
        property 'sonar.verbose', 'true'
        //property 'sonar.tests', "$projectDir\src\test\java"
        property 'sonar.binaries', "${buildDir}/classes/main/"

        // execute JaCoCo before the SonarQube run to have report file ready
        //property 'sonar.java.coveragePlugin', 'jacoco'
        property 'sonar.jacoco.reportPath', "${buildDir}/jacoco/test.exec"
        property 'sonar.junit.reportsPath', "${buildDir}/test-results"
    }
}

相关文章