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.8"
}
This will add testkit-gradle.properties
system resource.
- Create
gradle.properties
file used byGradleRunner
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')
}
}
Update from 1.0.5 or older
Starting from plugin version 1.0.6 the minimum supported Gradle version is 4.9