2 minutes
Gradle Multiple Test Sourceset Coverage
Problem
I want to categorise my tests, but I also want to see their respective code coverage using Jacoco.
Solution
Tested on Gradle v6.0.1.
When all your tests live in the test source set, you can simply follow the documentation. However, if you structure your tests more granularly, like this:
.
├── src
│ ├── cdcTest
│ │ ├── java
│ │ └── resources
│ ├── e2eTest
│ │ ├── java
│ │ └── resources
│ ├── intTest
│ │ ├── java
│ │ └── resources
│ ├── main
│ │ ├── java
│ │ └── resources
│ └── test
│ ├── java
│ └── resources
Then you will need to create additional sourcesets. I like to store these as build scripts, but you can put all this in one build.gradle if that suits your use case.
Then I apply this in build.gradle like so:
You can see in this example I have a few different types of test sources. To do the same it’s simply a rinse and repeat. I could use a list to generate multiple source sets programatically, but I prefer to be verbose here.
For code coverage we just need to update ./gradle/integration-test.gradle with the following block.