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.3"
}
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()
}
}
Custom JaCoCo destination file
JaCoCo destination file path defaults to ${project.buildDir}/jacoco/test.exec
but may be changed
using destinationFile
task property e.g.:
task generateJacocoIntegrationTestKitProperties(type: GenerateJaCoCoTestKitProperties) {
destinationFile = file('integration.exec')
}