I am new to gradle. I am using the below code. But it generates coverage for unit test cases. But it didn't generate for integration test cases. I have my test classes in the package src/test/java.
test {
dependsOn jettyRunWar
ignoreFailures true
finalizedBy jettyStop
}
apply plugin: 'jacoco'
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
}
Using Gradle 5.4.1 (and now 5.5.1), I was able to get a report after any test task, currently I have both
test
andintegrationTest
tasks.EDIT3: Fixed a potential bug when executing only some test tasks
executionData
indoLast
/doFirst
blocks, it was an error from my part. For more information checks this gradle github ticketdoLast
/doFirst
block)executionData { tasks.withType(Test).findAll { it.jacoco.destinationFile.exists() }*.jacoco.destinationFile }
EDIT2: The solution is the same, I just tweaked
jacoco.reportsDir
,tasks.withType(Test)
instead of just[test, integrationTest]
executionData
is done in thedoFirst
block instead ofdoLast
EDIT: After looking at the documentation of
JacocoReport
, there's a variant JacocoReport:executionData that take Gradle tasks directly. It works because the JaCoCo plugin adds aJacocoTaskExtension
extension to all tasks of typeTest
. Which is then less error prone.And the same trick can be applied to
sonarqube
task :Older but very working answer. Also using the knowledge above (that
Test
s task are extended byJacocoTaskExtension
) it's possible to replace the manualfile
configuration ofexecutionData
bytest.jacoco.destinationFile
andintegrationTest.jacoco.destinationFile
.I believe the most full answer will look like:
At least it's fully suited for my needs with integration and functional testing.
It seems like, what you need to tell build.gradle is where are your Intergration tests (i.e. folder containing those IT tests) using sourceSets. In my case, i have source under src/java (instead of src/main/java - gradle default).. my unit tests (Junit) under test/java folder, and my integration tests under src/java-test folder.
Then, I have integrationTest task as ... which you can tweak as you might not have cleanTest (custom task that i have created), so you can ignore that dependsOn... i think in your case you'll use something like jettyStart as you're using that for IT tests (starting the container for running IT tests and then finalizedBy feature to stop jetty .. jetty plugin)
SEE this post for more detailed output structure and script that I have at my end. Im getting the .exec for both Unit tests (test.exec) and IT tests intergrationTest.exec.. but Im not getting the jacoco.xml/jacocoHtml reports for both tests. I also found that, if I run "gradle clean build" (which includes call to "test" task) and "gradle clean build integrationTest" then, later one overwrites unit tests data in build/test-results folder and build/reports/tests folder.
Jacoco Unit and Integration Tests coverage - individual and overall
NOTE: in my case, jacocoTestReport is defined in the global gradle init.d folder in one of the common gradle file. This will help us not to include the same code in all / at project level build.gradle file.
Since I couldn't make it run with any of the answers, I will add my solution here. It will work if you run your test task (e.g.
integTest
) first and call thejacocoTestReport
afterwards.All you need is to tell the
jacocoTestReport
task where to find the gathered execution data from you test task. The execution data is always named after the test task. So if you have a test task calledintegTest
, your execution data will be stored inbuild/jacoco/integTest.exec
. ThejacocoTestReport
task can be configured to look for those other files too by adding them to the property executionData. You can also add wildcards includes so all execution data is taken into consideration:by executing the statement below the test coverage jacoco report will be created for you integration test task (e.g.
integTest
)This also works for multi module projects where you want to run the
integTest
task in modulea
: