Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0% Coverage in the SonarQube report for the Kotlin project

I'm setting up analyzing the a project by a SonarQube server. The used tool set is:

  • Kotlin 1.3.61
  • Gradle 6.0.1
  • Jacoco 0.7.9
  • SonarQube 7.5
  • SonnarQube Gradle Plugin 2.7

The problem is that I have 0.0% coverage in SonarQube, however, in the same time I have well formed jacoco test coverage report. Here is the snippets from build.gradle.tks:

 plugins {
    ...
    id("org.sonarqube") version "2.7"
}

sonarqube {
    properties {
        property("sonar.host.url", "http://localhost:9000")
        property("sonar.sources", "src/main/")
        property("sonar.tests", "src/test/")
        property("sonar.exclusions", "src/generated/")
        property("sonar.jacoco.reportPath", "build/jacoco/test.exec")
        property("sonar.junit.reportsPath","build/test-results/test")
        property("sonar.core.codeCoveragePlugin","jacoco")
        property("sonar.verbose", "true")
        property("sonar.binaries" ,"build/classes/kotlin")
        property("sonar.java.binaries" ,"build/classes/java, build/classes/kotlin")
        property("sonar.dynamicAnalysis", "reuseReports")
    }
}

jacoco {
    toolVersion = "0.7.9"
    reportsDir = file("${project.projectDir}/build/reports")
}

tasks.named("sonarqube") {
    dependsOn(tasks.named("jacocoTestReport"))
}

I have the next log's entries during executing ./gradlew sonarqube --info:

Task :jacocoTestReport Deleting stale output file: /home/xxx/project/build/reports/test/html Excluding [] Caching disabled for task ':jacocoTestReport' because: Build cache is disabled Task ':jacocoTestReport' is not up-to-date because: One or more additional actions for task ':jacocoTestReport' have changed.

[ant:jacocoReport] Loading execution data file /home/xxx/project/build/jacoco/test.exec

[ant:jacocoReport] Writing bundle 'project' with 43 classes Code coverage report: file:///home/xxx/project/build/reports/jacoco/test/html/index.html

:jacocoTestReport (Thread[Execution worker for ':',5,main]) completed. Took 0.398 secs. :sonarqube (Thread[Execution worker for ':',5,main]) started.

And:

Task :sonarqube

Load global settings
Load global settings (done) | time=48ms
Server id: BF41A1F2-AW2xHg7ddKb9ks8-VzQt
User cache: /home/xxx/.sonar/cache
Load/download plugins
Load plugins index
Load plugins index (done) | time=41ms
Load/download plugins (done) | time=52ms
Loaded core extensions: 
Process project properties
Execute project builders
Execute project builders (done) | time=2ms
Load project repositories
Load project repositories (done) | time=41ms
Load quality profiles
Load quality profiles (done) | time=24ms
Load active rules
Load active rules (done) | time=2524ms
Load metrics repository
Load metrics repository (done) | time=50ms
Project key: project
Project base dir: /home/xxx/project

-------------  Scan project
Base dir: /home/xxx/project
Working dir: /home/xxx/project/build/sonar
Source paths: src/main
Test paths: src/test
Source encoding: UTF-8, default locale: en_US
Index files
Excluded sources: 
  src/generated/

33 files indexed
15/15 source files have been analyzed
1/1 source files have been analyzed
CPD calculation finished

> Task :sonarqube
0 files ignored because of inclusion/exclusion patterns
Quality profile for kotlin: Sonar way
Quality profile for xml: Sonar way

Sensor JaCoCo XML Report Importer [jacoco]
Sensor JaCoCo XML Report Importer [jacoco] (done) | time=2ms
Sensor Kotlin Sensor [kotlin]
15 source files to be analyzed

Sensor Kotlin Sensor [kotlin] (done) | time=551ms
Sensor SonarJavaXmlFileSensor [java]
1 source files to be analyzed
Sensor SonarJavaXmlFileSensor [java] (done) | time=18ms
Sensor XML Sensor [xml]
Metric 'comment_lines_data' is deprecated. Provided value is ignored.
Sensor XML Sensor [xml] (done) | time=100ms
Sensor Zero Coverage Sensor
Sensor Zero Coverage Sensor (done) | time=14ms
9 files had no CPD blocks
Calculating CPD for 6 files
Analysis report generated in 82ms, dir size=138 KB
Analysis reports compressed in 28ms, zip size=50 KB
Analysis report generated in /home/xxx/project/build/sonar/scanner-report
Analysis report uploaded in 64ms
ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=project

Task total time: 4.773 s
:sonarqube (Thread[Execution worker for ':',5,main]) completed. Took 5.206 secs.

What's wrong with the configuration? Is something missed?

UPDATE:

Also I've found the next entries after executing gradle task with DEBUG mode:

[DEBUG] [org.sonarqube.gradle.SonarQubeTask] Sensors : JaCoCo XML Report Importer -> Kotlin Sensor -> SonarJavaXmlFileSensor -> XML Sensor -> Zero Coverage Sensor
[INFO]  [org.sonarqube.gradle.SonarQubeTask] Sensor JaCoCo XML Report Importer [jacoco]
[DEBUG] [org.sonarqube.gradle.SonarQubeTask] No reports found
[INFO]  [org.sonarqube.gradle.SonarQubeTask] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=2ms 
like image 929
PavelPraulov Avatar asked Jan 02 '20 16:01

PavelPraulov


1 Answers

Well, finally I've found this article:

  • coverage-test-data-importing-jacoco-coverage-report-in-xml-format

Due to it Version 5.12 of the Sonar Java analyzer deprecated use JaCoCo’s binary format (.exec files) to import coverage. This binary format is internal to the JaCoCo project, and as such there are no guarantees for backward compatibility, so it should not be used for integration purposes.

As a replacement, Sonarqube dev team developed the sonar-jacoco plugin, which imports JaCoCo’s XML coverage report, and this is the preferred option now.

So, to enable reading a Jacoco test coverage report I've changed the property from:

property("sonar.jacoco.reportPath", "build/jacoco/test.exec")

To:

property("sonar.coverage.jacoco.xmlReportPaths", 
         "build/jacoco/test/jacocoTestReport.xml")

And also I've added clause:

reports {
    xml.isEnabled = true
}

To the jacocoTestReport gradle task.

So, now I have in the log of :sonarqube Gradle task execution the following:

[DEBUG] [org.sonarqube.gradle.SonarQubeTask] Sensors : JaCoCo XML Report Importer -> Kotlin Sensor -> SonarJavaXmlFileSensor -> XML Sensor -> Zero Coverage Sensor [INFO] [org.sonarqube.gradle.SonarQubeTask] Sensor JaCoCo XML Report Importer [jacoco] [DEBUG] [org.sonarqube.gradle.SonarQubeTask] Reading report '/home/xxx/project/build/jacoco/test/jacocoTestReport.xml'

And correct test coverage percentage in the SonarQube report:

enter image description here

like image 112
PavelPraulov Avatar answered Nov 09 '22 15:11

PavelPraulov