Skip to content

koral--/jacoco-gradle-testkit-plugin

Repository files navigation

AppVeyor codecov Build Status

jacoco-gradle-testkit-plugin

Gradle plugin for JaCoCo code coverage in tests using Gradle TestKit.

Motivation

There is no built-it support for code coverage in TestKit. Those tests run in separate JVM and configuration of JaCoCo plugin is not taken into account. See Gradle forum post for more details.

Usage

  • Apply plugin in build.gradle:
plugins {
  id "pl.droidsonroids.jacoco.testkit" version "1.0.12"
}

This will add testkit-gradle.properties system resource.

  • Create gradle.properties file used by GradleRunner and populate it with content from mentioned resource. Sample kotlin code:
class AwesomeTest {

    fun InputStream.toFile(file: File) {
        use { input ->
            file.outputStream().use { input.copyTo(it) }
        }
    }

    fun GradleRunner.withJaCoCo(): GradleRunner {
        javaClass.classLoader.getResourceAsStream("testkit-gradle.properties").toFile(File(projectDir, "gradle.properties"))
        return this
    }

    @get:Rule
    val temporaryFolder = TemporaryProjectFolder()

    @Test
    fun `empty project builds successfuly`() {
        val result = GradleRunner.create()
                .withProjectDir(temporaryFolder.root)
                .withTestKitDir(temporaryFolder.newFolder())
                .withPluginClasspath()
                .withJaCoCo()
                .build()
    }
}

Adding coverage for another task (ex. integrationTest)

By default the plugin configures the test task for any project with Java plugin applied.

To configure the coverage for another task just add something like this:

jacocoTestKit {
    applyTo("intTestRuntimeOnly", tasks.named("integrationTest"))
}

Custom JaCoCo destination file

JaCoCo destination file path reads it from the JacocoTaskExtension so you can change it like this:

tasks.named("test").configure {
    jacoco {
        destinationFile = file('integration.exec')
    }
}

Requirements

Minimum supported versions:

  • Gradle: 7.6
  • Java: 1.8

Backwards compatibility

Migrating from 1.0.9 or older

Starting from version 1.0.10 the legacy plugin coordinates have changed. For example:

classpath("gradle.plugin.pl.droidsonroids.gradle.jacoco:jacoco-gradle-testkit-plugin:1.0.9")

became:

classpath("pl.droidsonroids.gradle.jacoco:pl.droidsonroids.gradle.jacoco:1.0.10")

Note the gradle.plugin prefix has gone. This change does NOT affect the plugins DSL:

plugins {
  id("pl.droidsonroids.jacoco.testkit") version "1.0.10"
}