Now with the release of JUnit 5.0 I'm struggling to get code coverage data using jacoco.
My project is multi-module and my problem is that there is no exec file created for each module. Only one in the root project (which seems almost empty). I've not been able to find an up-to-date guide for this.
I've tried different approaches described here: Gradle Jacoco and JUnit5 without any success.
Does anyone have a working setup in a multi-module (non-Android) Gradle project with JUnit5 and Jacoco?
Or a Gradle Android project with JUnit5 and Jacoco? Any advice is greatly appreciated. Even ugly hacks are welcome until there is some official documentation available.
Relevant part of my code (where running JUnit5 works both in commandline and from the IDE at least):
<Root> build.gradle:
buildscript {
dependencies {
...
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0"
}
}
...
apply plugin: 'org.junit.platform.gradle.plugin'
ext.junitVintageVersion = '4.12.0'
ext.junitPlatformVersion = '1.0.0'
ext.junitJupiterVersion = '5.0.0'
subprojects {
apply plugin: "jacoco"
jacoco {
toolVersion = "0.7.6.201602180812"
}
...
junitPlatform {
filters {
engines {
include 'junit-jupiter', 'junit-vintage'
}
tags {
exclude 'slow'
}
includeClassNamePatterns '.*Test', '.*Tests'
}
}
}
...
dependencies {
// org.junit.platform.commons.util.PreconditionViolationException:
// Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
testCompile("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
}
For non-Android projects these approaches look good.
But anyway below is a complete working example of non-Android project.
Be aware that Android projects are completely different beasts compared to non-Android, including but not limited to the configuration/execution of tests and computation of coverage.
In particular - by default JaCoCo library (http://www.jacoco.org/jacoco/index.html) changes classes for recording of coverage "on-the-fly" during execution of application using Java agent. And this mode is exactly what default JaCoCo Gradle Plugin (https://docs.gradle.org/4.1/userguide/jacoco_plugin.html) provides.
Java agents can't be used on Android, and so for Android change of classes happens "off-line" (http://www.jacoco.org/jacoco/trunk/doc/offline.html) during build by Android Gradle Plugin (https://developer.android.com/studio/releases/gradle-plugin.html).
Also seems that
org.junit.platform:junit-platform-gradle-plugin
is not applicable for Android projects - there is https://github.com/aurae/android-junit5 and for the time being open discussion about coverage at https://github.com/aurae/android-junit5/issues/4Not being a daily developer of Android projects, I can't provide more information than this.
settings.gradle
:build.gradle
:a/src/main/java/A.java
:a/src/test/java/ATest.java
:b/src/main/java/B.java
:b/src/test/java/BTest.java
:After execution of
gradle test jacocoTestReport
filesa/build/jacoco/test.exec
andb/build/jacoco/test.exec
are generated as well reports shown below.a/build/reports/jacoco/test/html/index.html
:b/build/reports/jacoco/test/html/index.html
: