Michael Zöller
Michael Zöller

Reputation: 125

Cross-module code coverage with jacoco and gradle multi-module project

We use gradle 3.3 and jacoco tool verson 0.7.6.201602180812. We have a gradle multi-project like this:

We use unit-tests testing the project sources and jacoco on all child-projects producing test.exec files. We have additional integration-tests in the int-test project adding jacoco results to the test-exec in the int-test project. We use sonarqube gradle plugin (2.2.1) on the parent project to collect everything for a SonarQube server v6.2.

Everything runs fine with tests that test sources in their own project: The code coverage is measured in the jacoco reports as well as on SonarQube.

Only the integration test (int-test project) coverage for the sources in the prod-projects (single process) is not measured neither in the coverage report in the project with the test nor in the project with the class.

Probably one needs to combine the coverage data on the top level project somehow - does anyone know how to do that? At best with SonarQube still showing the coverage on single module level as well.

EDIT Here is a small test project: https://github.com/MichaelZett/coveragetest Running
'build smokeTest sonarqube' leads to:

Upvotes: 11

Views: 18988

Answers (4)

Artur  Dumchev
Artur Dumchev

Reputation: 1330

I have managed to fix the issue by doing this:

  1. The the build.gradle file of the module that has all the test (lets call it "main"):
apply plugin: 'jacoco'

jacoco {
    toolVersion = jacoco_tool_version
}

jacocoTestReport {

    // this paragraph below was added to inculde submodule sources
    def  modules = [
            ':submodule_dir:module1_name',
            ':submodule_dir:module2_name',
    ]
     modules.forEach {  module ->
        additionalSourceDirs.from(files(project(module).file('src')))
        sourceDirectories.from(files(project(module).file('src')))
        classDirectories.from(files(project(module).projectDir.path))
    }

    reports {
        xml.required = true
    }
}

sonar {
    properties {
        property "sonar.coverage.jacoco.xmlReportPaths", "backend/build/reports/jacoco/test/jacocoTestReport.xml"
    }
}
  1. On the project root build.gradle file:
plugins {
    id "org.sonarqube" version "$sonarqube_version"
}

def fallBackReportPaths = "$projectDir/main/build/reports/jacoco/test/jacocoTestReport.xml"

subprojects {

    sonar {
        properties {
            // this was added to include ReportPaths to all the modules (module1_name, module2_name)
            property "sonar.coverage.jacoco.xmlReportPaths", fallBackReportPaths
        }
    }
}

Upvotes: 0

Cristian
Cristian

Reputation: 200080

As noted in the comments, you must first merge the Jacoco execution data and then tell sonarqube to use that instead of the individual exec files generated by each submodule.

I'm adding an example here since the links provided in the accepted answer are a little bit misleading. Most of them provide you with different workarounds to merge Jacoco reports, not to merge the execution data, which is what you want.

Here's how it would look like:

def allTestCoverageFile = "$buildDir/jacoco/allTestCoverage.exec"

sonarqube {
    properties {
        property "sonar.projectKey", "your.org:YourProject"
        property "sonar.projectName", "YourProject"
        property "sonar.jacoco.reportPaths", allTestCoverageFile
    }
}

task jacocoMergeTest(type: JacocoMerge) {
    destinationFile = file(allTestCoverageFile)
    executionData = project.fileTree(dir: '.', include:'**/build/jacoco/test.exec')
}

task jacocoMerge(dependsOn: ['jacocoMergeTest']) {
    // used to run the other merge tasks
}

subprojects {
    sonarqube {
        properties {
            property "sonar.jacoco.reportPaths", allTestCoverageFile
        }
    }
}

In a nutshell:

  • First, we define a global coverage file output for our test reports (allTestCoverageFile).
  • Then we need to tell Sonarqube to use that file (using sonar.jacoco.reportPaths). But notice we also have to do it in the subprojects closure. This is extremely important. Don’t miss it.
  • Finally, we create a custom task that extends from JacocoMerge (an incubating class from the Jacoco plugin), that merges all the test coverage reports from all projects (executionData) into our allTestCoverageFile.

If you are using a version of SonarQube prior to 6.2 please use sonar.jacoco.reportPath property

Upvotes: 12

Ashwini Mutalik Desai
Ashwini Mutalik Desai

Reputation: 91

subprojects {
    apply(plugin: 'org.jetbrains.kotlin.jvm')

    repositories {
        jcenter()
        mavenCentral()
   }
}

task codeCoverageReport(type: JacocoReport) {

    // Gather execution data from all subprojects
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    // Add all relevant sourcesets from the subprojects
    subprojects.each {
        sourceSets it.sourceSets.main
    }

    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
    }
}

// always run the tests before generating the report
codeCoverageReport.dependsOn {
    subprojects*.test
}

sonarqube {
    properties {
        property "sonar.projectKey", "your_project_key"
        property "sonar.verbose", true
        property "sonar.projectName", "Your project name"
        property "sonar.coverage.jacoco.xmlReportPaths", "${rootDir}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
    }
}

Command to run test with coverage:

./gradlew codeCoverageReport
./gradlew sonarqube -x test (test is excluded since already run and sonarqube by default executes test)

Two things to be noted that made it work:

  1. To make available sourcesets of all modules, looping over subprojects and accumulating sourcesets worked. subprojects.sourceSets.main.allSource.srcDirs did not work.
  2. sonar.jacoco.reportPaths is deprecated. We need to use sonar.coverage.jacoco.xmlReportPaths. Check the documentation here

Upvotes: 2

Godin
Godin

Reputation: 10564

Speaking about SonarQube: you can get aggregated report by using same location for jacoco.exec across all modules. Make sure that file is removed before build and appended in all modules.

Speaking solely about Gradle: have a look on

Upvotes: 6

Related Questions