2

We would like to use Gradle in conjunction with Kotlin and Jacoco (+JUnit 5) for generating a code coverage report.

Our project directory tree looks as follows:

project/{src,test}/main/kotlin ...

Our build.gradle file looks as follows:

jacoco {
    toolVersion = "0.7.9"
    reportsDir = file("$buildDir/reports")
    applyTo junitPlatformTest
}
jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage report."
    classDirectories = fileTree(
            dir: "$buildDir/classes/kotlin/main"
    )
    def coverageSourceDirs = [
            "src/main/kotlin"
    ]
    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/junitPlatformTest.exec")
    reports {
        xml.enabled = true
        html.enabled = true
        csv.enabled = true
    }

}
test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/junitPlatformTest.exec")
        includeNoLocationClasses = true
    }
}
test.dependsOn junitPlatformTest

With this configuration, Jacoco generates a html report and puts it under build/reports/test/html. However, it shows me 0% coverage. This should not be the case, because I have one test case that excercices all methods for a single dummy class in the project.

I went through several posts such as:

However, I could not quite find a solution that worked for me, yet.

Julian
  • 1,694
  • 22
  • 29
  • This is odd... after renaming my JUnit test from TestXY to XYTest it seems to work. I must have missed some constraints regarding the naming convention. – Julian May 18 '18 at 14:50
  • 1
    The `junit-platform-gradle-plugin` uses a standard include pattern unless configured explicitly: https://junit.org/junit5/docs/current/api/org/junit/platform/engine/discovery/ClassNameFilter.html#STANDARD_INCLUDE_PATTERN – Marc Philipp May 19 '18 at 11:25
  • Thanks a lot Marc! I have missed that point in the documentation. – Julian May 20 '18 at 09:28
  • 1
    This link helped me: https://kevcodez.de/index.php/2018/08/test-coverage-in-kotlin-with-jacoco/ – Marcin Stachniuk Sep 29 '18 at 19:31
  • 1
    The URL that Marcin Stachniuk mentioned is now https://kevcodez.de/posts/2018-08-19-test-coverage-in-kotlin-with-jacoco/ – Ari Lacenski Oct 23 '19 at 23:03
  • Worth a try https://stackoverflow.com/questions/68194456/jacoco-code-coverage-0-in-android-kotlin-project/69051614#69051614 – BabyishTank Sep 04 '21 at 00:37

1 Answers1

0

I recently hit this issue when upgrading form junit 4 -> 5. In my case I had to re-arrange the declaration of the dependencies as outlined here. If you change to this format you should see the coverage reported correctly again without having to change any other configuration.

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
Celt
  • 2,469
  • 2
  • 26
  • 43