Seems that while Gradle JaCoCo Plugin enhances testNG
task, so that its execution uses JaCoCo Java agent, but it forgets to update jacocoTestReport
task, so that this task doesn't use results of execution of testNG
task. Don't know if this is a bug or on purpose, but solution is provided below.
to demonstrate this
file src/main/java/Example.java
:
public class Example {
public void junit() {
System.out.println("JUnit");
}
public void testng() {
System.out.println("TestNG");
}
}
file src/test/java/ExampleJUnitTest.java
:
import org.junit.Test;
public class ExampleJUnitTest {
@Test
public void test() {
new Example().junit();
}
}
file src/test/java/ExampleTestNGTest.java
:
import org.testng.annotations.Test;
public class ExampleTestNGTest {
@Test
public void test() {
new Example().testng();
}
}
file build.gradle
:
apply plugin: 'java'
apply plugin: 'jacoco'
repositories {
mavenCentral()
}
dependencies {
testCompile 'org.testng:testng:6.8.8'
testCompile 'junit:junit:4.12'
}
task testNG(type: Test) {
useTestNG()
}
test {
dependsOn testNG
}
After execution of gradle clean test jacocoTestReport -d
you'll see in log
java ... -javaagent:.../jacocoagent.jar=destfile=build/jacoco/testNG.exec ...
...
java ... -javaagent:.../jacocoagent.jar=destfile=build/jacoco/test.exec ...
and that directory build/jacoco
contains two files - testNG.exec
and test.exec
, for testNG
and for test
tasks respectively. While JaCoCo report shows only execution of JUnit by test
task.
to solve this
Either instruct task testNG
to write execution data into same file as test
:
task testNG(type: Test) {
useTestNG()
jacoco {
destinationFile = file("$buildDir/jacoco/test.exec")
}
}
Either instruct task jacocoTestReport
to also use testNG.exec
file:
jacocoTestReport {
executionData testNG
}
I'm assuming that the same should be done for the case of multi-module project in general and in your case in particular, since Minimal, Complete, and Verifiable example of your multi-module project setup wasn't provided.