Code Quality Basics - What Is Code Coverage?


In this article, we find out what code coverage is, and how we keep track of it.

You will learn

  • What Is Code Coverage?
  • How do you measure Code Coverage?
  • How can you ensure that you have great unit tests?
  • How do you ensure that you have great asserts in tests in addition to code coverage?
  • How can ensure that Code Coverage standards are Adhered to?

Article Series

This is fifth article in a series of 6 articles on important code quality terminology:

What Is Code Coverage?

Unit Tests Are Important

A prerequisite for great code is having a set of great unit tests.

Adhering to test driven development leads us to create great code.

TDD ensures we have great low level design, and the set of unit tests we write tend to be reliable.

Understanding Code Coverage

The result of great unit tests is great code coverage. Code coverage is a measure of what percentage of the lines of source code that are covered by unit tests.

For example, if your project has 1000 lines of source code, unit tests cover 900 lines, then the code coverage of your application is 90%. Typically, 85-90% is considered to be good code coverage.

Exploring Code Coverage

Have a look at the snapshot of the SonarQube Dashboard:

image info

The project being analyzed is being reported to have a code coverage of 97.1%. This means 97.1% of the source code is being covered by unit tests execution. That’s awesome.

You can also see a ring beside the number estimate, as well as a shaded graph as the background. The project started off with approximately 80% coverage, and slowly built it up to the present figure.

When you click on the link, you can see several source files, and their individual coverage:

image info

You can see that a few of the files do not have any unit test written for them at all. These are the files with 0% coverage.

Let’s pick the file ListTreeImpl.java as an example: image info

This file has a coverage of around 72.1%. When you look at the code inside the file, you will see several vertical green bars. These indicate block of code that are fully covered by unit tests. The lines in red indicate those code blocks that are not covered by unit tests: image info

Why Is Code Coverage Important?

Good unit tests can catch defects that could arise out of buggy code, even in the future! They are a kind of insurance policy for developers. These tests are automated, and keep running every time code is changed.

Code coverage is a good measure to identify if all scenarios are being unit tested.

Find Defects Early

The more number of tests you write, the less defects you will have in the future. Having great unit tests, gives you the confidence to refactor your code. Refactoring helps improve the design of the code over a period of time.

Challenges With Code Coverage

Here are a few challenges one has to face while dealing with code coverage.

Writing Tests Just For Coverage

Many project teams make the mistake of having code coverage as a direct measure. If the focus is only on the number of lines covered by unit tests, the quality of tests could get neglected.

Code coverage needs to be treated just as a result of great unit tests.

Having great code coverage does not mean you have great unit tests. If you end up writing tests that don’t check the business logic, but only increase the lines of code covered, the purpose is defeated.

The focus needs to be on a combination of the quality of the unit tests written, plus the number of lines of code covered.

Dealing With Legacy Code

In general, legacy code components are not loosely coupled, and hence writing unit tests for them becomes quite a challenge. You might need to refactor code to enable you to write unit tests.

Most of the old frameworks - mainly the web and database ones - are not designed to be unit tested.

Measuring Code Coverage

Use Sonar

SonarQube is one of the most popular static analysis tools that is used to measure code coverage.

It is also possible to connect to SonarQube from an Eclipse plugin, and look at the measures from an Eclipse view itself.

Use IDE Plugins

You can use Eclipse IDE plugins such as Cobertura and EclEmma to measure code coverage.

The IntelliJ IDE has in-built support to measure an application’s code coverage.

Best Practices For Code Coverage

Include Coverage In Definition Of Done

Make sure that you constantly review your code coverage measures at the end of each sprint. Also ensure that the quality of asserts are reviewed.

Measure Coverage From Day One

You need to be committed to measuring code coverage from the day of inception of the project. For that, you write unit tests for your code from the point where you start the prototype, and when you start developing the vertical slice as well.

Do check out our video on the same topic:

image info

Summary

Code coverage measures the lines of code covered by unit tests. Having good unit tests is important for any project, as they act as a safety net against defects in the future. Code Coverage can be measured by tools such as SonarQube, or common IDE plugins.

10 Step Reference Courses

Image Image Image Image Image

in28Minutes is creating amazing solutions for you to learn full stack and the cloud - Docker, Kubernetes, AWS, React, Angular etc. Click here for the complete catalogue of 30 Courses.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Software Design - Separation Of Concerns - with examples

Software architects and programmers love having Seperation of Concerns. What is it? Why is it important? Let's get started.

Object Oriented Software Design - Solid Principles - with examples

Software design is typically complex. Object oriented design takes it to the next level. There are a number of design patterns and other stuff to be aware of. Can we make things simple? What are the goals to aim for when you are doing object oriented design? SOLID Principles is a great starting point for Object Oriented Design.

Software Design - Open Closed Principle - with examples

Open Closed Principle is one of the SOLID Principles. You want your code to be easily extended. How do you achieve it with minimum fuss? Let's get started.

Software Design - What is Dependency Inversion Principle?

Dependency Inversion Principle is one of the important SOLID Principles. Dependency Inversion Principle is implemented by one of the most popular Java frameworks - Spring. What is it all about? How does it help you design good applications?

Introduction to Four Principles Of Simple Design

With agile and extreme programming, the focus is on keeping your design simple. How do you keep your design simple? How do you decide whether your code is good enough?

Software Design - Single Responsibility Principle - with examples

For me, Single Responsibility Principle is the most important design principle. What is Single Responsibility Principle? How do you use it? How does it help with making your software better? Let's get started.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Designing REST API - What is Code First Approach?

Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?