简体   繁体   中英

sonarqube+jacoco+junit code coverage showing 0% in sonarqube dashboard

I am using sonarqube + jacoco for codecoverage, i have junit test case for my java code in below dir structure.

src/main/java/code/abc.java
src/main/test/code/Testabc.java

I want a code coverage on sonarqube dashboard and i am using gradle for the same.

apply plugin: "java"
apply plugin: "org.sonarqube"
apply plugin: "jacoco"

jacoco {
toolVersion = "0.7.9"
reportsDir = file("${project.buildDir}/jacoco/customJacocoReportDir")
}

sonarqube {
    properties {
        property "sonar.sources", "."
        property "sonar.projectName", "javacode"
        property "sonar.projectKey", "javacode"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.java.binaries", "/data/tmp/env/"
        property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
    }
}

buildscript {

   repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"

  }
}

I am running gradle sonarqube

I dont know where i am doing wrong? if any one can help to get the coverage of test case written from java code.

It seems to me, that Jacoco test reports are generated in one directory, but SoanrQube is configured to look into another one. Here is the directory you save reports:

reportsDir = file("${project.buildDir}/jacoco/customJacocoReportDir")

and here is Sonar configuration to look up Jacoco results:

property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"

There is customJacocoReportDir directory missed

Here's the minimal config that is working with

  • gradle 7.5
  • sonarqube gradle plugin 3.3
  • jacoco latest version

Gradle snippet:

apply plugin: "org.sonarqube"
apply plugin: "jacoco"

// jacoco task config not needed since I'm using the defaults (latest version, default dir)
// jacoco {
//     toolVersion = "0.7.9"
//     reportsDir = file("${project.buildDir}/jacoco/customJacocoReportDir")
// }

sonarqube {
    properties {
        property "sonar.sources", "."
        property "sonar.projectName", "javacode"
        property "sonar.projectKey", "javacode"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.java.binaries", "/data/tmp/env/"
//      no need to define sonar.jacoco.reportPaths or sonar.coverage.jacoco.xmlReportPaths as sonarqube 
//      finds the xml report in the default location (build/reports/jacoco/test/jacocoTestReport.xml)
//      property "sonar.jacoco.reportPath", "${project.buildDir}/jacoco/test.exec"
    }
}

jacocoTestReport {
    reports {
        xml.enabled true   // to be sure xml report is created (sonarqube knows jacoco's default report location)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM