Code Quality Basics - What Is Code Coverage?

Image Image

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?

Free Courses - Learn in 10 Steps

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 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


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.

Best Selling Udemy Courses

Image Image Image Image Image Image Image Image Image Image Image Image

Join 450,000 Learners and 30+ Amazing Courses

350,000 Learners are learning everyday with our Best Selling Courses : Spring Boot Microservices, Spring, Spring Boot, Web Services, Hibernate, Full Stack React, Full Stack Angular, Python, Spring Interview Guide, Java Interview, Java Functional Programming, AWS, Docker, Kubernetes, PCF, AWS Fargate and Azure

Do not know where to start your learning journey? Check out our amazing learning paths:
Learning Path 01 - Spring and Spring Boot Web Applications and API Developer,
Learning Path 02 - Full Stack Developer with Spring Boot, React & Angular,
Learning Path 03 - Cloud Microservices Developer with Docker and Kubernetes,
Learning Path 04 - Learn Cloud with Spring Boot, AWS, Azure and PCF and
Learning Path 05 - Learn AWS with Microservices, Docker and Kubernetes



Related Posts

Understanding jwt token

Let us understand the json web tokens

Single sign-on workflow

Let us understand the single sign-on workflow

Oauth2.0 - Resource Owner Password Credentials grant workflow

Let us understand the Oauth2.0 Resource Owner Password Credentials grant workflow

Oauth2.0 - Implicit grant workflow

Let us understand the Oauth2.0 implicit grant workflow

Oauth2.0 - Client Credentials grant workflow

Let us understand the Oauth2.0 client credentials grant workflow

Oauth2.0 - Authorization grant workflow

Let us understand the Oauth2.0 authorization grant workflow

Writing Integration Tests for Rest Services with Spring Boot

Setting up a basic REST Service with Spring Boot is a cake walk. We will go one step further and add great integration tests!

Integrating Spring Boot and Spring JDBC with H2 and Starter JDBC

Learn using Spring Boot Starter JDBC to connect Spring Boot to H2 (in memory database) using Spring JDBC. You will create a simple project with Spring Boot. You will add code to the project to connect to a database using Spring JDBC. You will learn to implement the basic CRUD methods.

JUnit Tutorial for Beginners in 5 Steps

JUnit Tutorial for Beginners in 5 Steps. Setting up a basic JUnit example and understanding the basics of junit.

JPA and Hibernate Tutorial For Beginners - 10 Steps with Spring Boot and H2

JPA and Hibernate in 10 Steps with H2 - Setting up a basic project example with Spring Boot and in memory database H2. Its a cake walk.