A Gradle plugin that enables JaCoCo coverage collection for Gradle TestKit's GradleRunner tests.
Apply the plugin at your java-gradle-plugin project:
plugins {
`java-gradle-plugin`
id("io.github.gmazzo.gradle.testkit.jacoco") version "<latest>"
}Then, on each test using GradleRunner,
also apply the jacoco-testkit-coverage plugin at either root's build.gradle or settings.gradle:
class MyPluginFunctionalTest {
@get:Rule
val temporaryFolder = TemporaryFolder()
@Test
fun myPluginTest() {
temporaryFolder.root.resolve("settings.gradle").writeText(
"""
plugins {
id("jacoco-testkit-coverage") // this will dump coverage data
}
rootProject.name = "test-project"
""".trimIndent()
)
val result = GradleRunner.create()
.withProjectDir(temporaryFolder.root)
.withPluginClasspath()
.withArguments("myTask")
.build()
}
}Note
The jacoco-testkit-coverage is an internal plugin added by this TestKit extension.
It can be either applied to the root Project's script or to the Settings script
The plugin modifies java-gradle-plugin's
pluginUnderTestMetadata classpath (the one computed by GradleRunner.withPluginClasspath
by using JaCoCo's Offline Instrumentation
Also, a jacoco-testkit-coverage plugin also added to withPluginClasspath, allowing to dump the coverage data after each build is run.