It is still true that using withDebug(true)
would work to get coverage as the tests are run in-process, but it is also true that you can then for example not use withEnvironment
.
I actually set this up some days ago to work properly without needing to do withDebug
.
I basically do something very similar to what the plugin linked in OP here is doing, I just didn’t like too much how it is done there and had needed infrastructure already anyway, that is a base class for the functional tests that prepares a base build I extend in the individual tests, so was able to just write the gradle.properties
there as needed.
I
- create a resolvable configuration
jacocoAgentJar
- add as dependency
org.jacoco:org.jacoco.agent:...:runtime
- set
jacocoAgentJar.singleFile.absolutePath
as the value of a system property for the test task - set
the<JacocoTaskExtension>().destinationFile!!.absolutePath
as the value of another system property for the test task - add a
doLast { ... }
action to the test task that gets the read lock on the JaCoCo result file to wait for the test having written to it likedoLast { // wait for the read lock on the file, otherwise the daemon might still // have the write lock and Gradle complains that it cannot snapshot // the file as output of this task and as input of the report task FileChannel.open(jacocoDestfile.toPath(), READ).use { it.lock(0, Long.MAX_VALUE, true).release() } }
- and in the base class for the tests I prepare the
gradle.properties
file with
and writing to the filedef jacocoAgentJar = System.getProperty('jacocoAgentJar').replace($/\/$, $/\\/$) def jacocoDestfile = System.getProperty('jacocoDestfile').replace($/\/$, $/\\/$)
org.gradle.jvmargs = -javaagent:$jacocoAgentJar=destfile=$jacocoDestfile
This seems to work fine so far.