Build.gradle with Jacoco plugin does not generate coverage report for integration tests

I have a build.gradle file that can successfully run unit and integration tests separately or together (with a command like gradle test integrationTest for sharing). Both use Junit, I use Gradle 3, and this is not an Android project. A success report is created for everyone in separate directories. I can also successfully create a Jacoco report to cover unit test with gradle test jacoco . I cannot get a coverage report for my other working integration tests using gradle integrationTest jacoco , although the test succeeds and the integrationTest.exec file is created.

To be more explicit, I get a unit test coverage report in the build / reports / index.html file, and Junit reports in the build / reports / test / index.html and build / reports / integrationTest / index.html file. The build / reports / jacoco directory is created, but contains only an empty "test" directory. build / also contains the jacoco / directory, which contains the .exec and classpathdumps files.

Here is my abbreviated build.gradle

apply plugin: 'java' apply plugin: 'application' apply plugin: 'jacoco' repositories { mavenCentral() } sourceSets { main { java { srcDirs = ['src/java'] } test { java { srcDirs = ['test/java'] } } resources { srcDirs = ['src/java', 'conf', 'resource'] } } integrationTest { java { compileClasspath += main.output + test.output runtimeClasspath += main.output + test.output srcDir file('integration_test/java') } } } test { jacoco { append = false destinationFile = file("$buildDir/jacoco/jacocoTest.exec") classDumpFile = file("$buildDir/jacoco/classpathdumps") } } configurations { integrationTestCompile.extendsFrom testCompile integrationTestRuntime.extendsFrom testRuntime } /* SNIP */ task integrationTest(type: Test) { dependsOn startserver testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath } integrationTest.finalizedBy stopserver check.dependsOn integrationTest integrationTest.mustRunAfter test tasks.withType(Test) { reports.html.destination = file("${reporting.baseDir}/${name}") } jacoco { toolVersion = "0.7.6.201602180812" } jacocoTestReport { reports { xml.enabled false csv.enabled false html.destination "$buildDir/reports" } } 

I saw existing questions about merging two reports, creating reports on Junit integration integration that I already have, and similar, but ultimately useless questions about Maven and Ant. The closest I found was, but nothing that I tried to adapt from it was fruitful. It seems that there is a similar question but with fewer of their build.gradle, and the only answer is the invalid answer 0-up-vote the author of the question in the previous link.

Since this is already quite long, I did not want to over-provide even more than here, but I am happy to provide more if something is unclear.

I confirmed that nothing stupid as a unit test results of rewriting integration tests takes place - running integration tests and jacoco without regular tests does not even create similar files.

Is there anything I can do to get the integration test covered?

[First edit] I created a small Github repo that has everything needed to reproduce this problem: https://github.com/micseydel-tile/gradle-jacoco-integration-test

+5
source share
1 answer

You can force both test tasks to write to the same destination file, and they are added to the existing file, if any. Thus, you can run any of the test tasks and view the coverage separately in the HTML report. This snippet of code from your little github repit

 `task integrationTest(type: Test) {jacoco { append = true destinationFile = file("$buildDir/jacoco/jacocoTest.exec") classDumpFile = file("$buildDir/jacoco/classpathdumps") } testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath}` 

you can just add the jacoco code block and enable append.

+1
source

Source: https://habr.com/ru/post/1257995/


All Articles