Code Coverage with Gradle and Jacoco

Introduction

Samuel Addico
Sep 17, 2018 · 3 min read

So recently, I have was tasked to set up a Continuous Delivery pipeline for a number of applications at my place of work. The pipeline consisted of a number of steps including Checkout, Test, Code Coverage, Maven/Gradle Build, Push to Artifactory, Deploy to Stage Env and send a slack notification to the team. That's is pretty long, right? lol. I will probably blog on this later, but for today I will be sharing how I did the code coverage bit with Jacoco plugin. There are other tools like Cobertura and Sonarqube that serve the same purpose.

What is Code Coverage?

Code coverage is a measure of how much for your application’s code has been executed in testing. This covers not only seeing which lines of code have been executed but also checking that all branches have been covered. Code coverage is also called Test coverage. This gives developer teams reassurance that their programs have been broadly tested for bugs and should be relatively error-free.

What I did

Now let's zoom into action. So starting from Gradle 1.6, Jacoco plugin is supported, you just have to enable it. To enable it, add below in your build.gradle file.

apply plugin: 'jacoco'

Below is how the HTML output looks like

Now let come to them, even more, sweeter part of the cake:- enforcing code coverage metrics. What this means is that a threshold or rule is set for which a gradle task can be used to verify if code coverage metrics are met based on configured rules/threshold. The build fails if any of the configured rules are not met. JaCoCo only reports the first violated rule. See the example below.

jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.7
}
}
}
}

When the jacocoTestCoverageVerification task is run, this rule will ensure that a code coverage metric of 70% met to be able to pass the build. To run coverage verification during the build (and fail when appropriate), you will need to add below again the build.gradle file

check.dependsOn jacocoTestCoverageVerification

Below is an example screenshot of a failed build.

This brings the curtains down on this session, I hope this helps someone out there. Feel free to leave a comment or correction. The source code for this session is hosted on my GitHub repository. Bye 🙂

References

Gradle Jacoco plugin

Originally published by samaddico at http://hashcod.wordpress.com on September 17, 2018.

CodeOps

The junction is a technology intersection devoted to…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store