Nowadays software development process aims for automatization in each possible area from daily deployment to automated task creation in JIRA. To be able to apply such methods during development we have to start from writing as many good tests as possible. Additionally, we should write possibly fast tests. First of all, to be able to do this we need to create a good architecture of the application.  To determine if our code is tested enough we can check something like code coverage.

What is code coverage?

Code coverage is a measure that describes how much of the source code in an application is executed when running a particular test suite. Looking at reports we can see which packages are tested enough which one not.
Report of Code coverage
When opening particular classes we will see exactly which parts of code don’t run. It can tell us which test cases left to write to be 100% sure that code works as expected.
Code coverage report
Yellow color tells us that there is no test that runs this condition enough times. This means that probably we wrote a test that checked only one condition. Red color tells, that our test doesn’t run this part of source code.

Which benefit brings this approach?

There are many benefits of a code coverage measurement. Here are three benefits that were the most important in my previous projects.

  1. We are able to automatically prevent pushing untested code.
  2. We can see nice reports that help us to determine untested code
  3. Reports are good for QA managers to prove that we take care of quality.

What is bad in such approach?

Many people rely on this measurement too much. Very often companies have in agreement that code coverage ratio should be 80%. But what it means? Why 80% not 90% or 100%? This leads often to a situation that programmers have to write code only to improve coverage. That’s the reason why we sometimes see a stupid test that checks completely noting or test tests itself. We could also write tests without asserts only to go through untested code. It’s worth remembering that a healthier approach is not to set an exact cover ratio only to cultivate an idea of 80% coverage. Due to the above, programmers should be encouraged to write good functional tests and we should tread coverage like advice not like a whip.

Code coverage tools

To measure code coverage I recommend commonly known tool called Jacocco. Reports that I pasted above are the result of coverage analysis that Jacocco does for us. Because of maturity of this tool it’s documentation is clear and easy to understand, use and configure. Jacocco can be used within build tools like a Maven and Gradle. We can configure this plugin is such way, that when coverage has cover ratio less that set amount build will fail. In such way CI tools can prevent merging untested code to code repository.

Summary

The code coverage is a really helpful thing during software development. I cannot imagine working without it. It reminds me often, that I forgot to test something, that turned out later really crucial.  Having this measurement development team has to understand that this should help us to create good software instead of beeing only input for higher-management reports.

About author

Hi,
my name is Michał. I’m software engineer. I like sharing my knowledge and ideas to help other people who struggle with technologies and design of todays IT systems or just want to learn something new.
If you want to be up to date leave your email below or leave invitation on LinkedIn, XING or Twitter.

Add your email to be up to date with content that I publish


1 Comment

Mike Primas · 10/28/2019 at 21:33

If some one needs expert view concerning blogging after that i recommend him/her to visit this blog, Keep up the good job.|

Leave a Reply

Your email address will not be published. Required fields are marked *