Code Quality Basics - What Is Code Coverage?


NEW COURSE RELEASED! - 21 HOURS, 200+ VIDEOS - Master DevOps with Docker, Kubernetes and Azure DevOps


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

What to Learn Next?

NEW COURSE RELEASED! - 21 HOURS, 200+ VIDEOS - Master DevOps with Docker, Kubernetes and Azure DevOps


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


85,000 Subscribers are learning from our Free Videos on YouTube : JSP Servlets, Spring, Spring Boot, Spring MVC, Hibernate, Eclipse, Maven, JUnit, Mockito, Full Stack - React, Full Stack - Angular, Docker, Kubernetes, AWS, AWS Fargate, PCF and Azure


Here are the recommend articles to read next : Spring Interview Questions, Spring Boot Interview Questions, Microservices, Hibernate, Spring Security, REST API with Spring Boot, Full Stack with React, SOAP Web Services, Exception Handling, Embedded Servers, Spring Data Rest, Spring vs Spring MVC vs Spring Boot, Building Web Application and Spring Data JPA. You can checkout all our 100+ articles here - All Articles.


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

Docker Tutorial for Beginners - with Java and Spring Boot

Learn about Docker - What is Docker? Why Is Docker Popular? How to create Docker Image for a Java Spring Boot App?

Devops Tutorial | DevOps with Docker, Kubernetes and Azure DevOps

What is DevOps? How is it different from Agile? What are the popular DevOps Tools? What is the role of Docker, Kubernetes and Azure DevOps in DevOps. Let's get started with a simple usecase.

Deploy Java Spring Boot Applications to AWS, Azure, GCP with Docker and Kubernetes

In this article, we focus our attention on the cloud. How to learn the cloud and deploy Java Spring Boot Applications to AWS, Azure, GCP with Docker and Kubernetes?

Microservices Architectures - Non Functional Requirements - Performance

In this article, we explore an important non functional requirement called performance.

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.

Microservices with Spring Boot and Java - Part 1 - Getting Started

Let's learn the basics of microservices and microservices architectures. We will also start looking at a basic implementation of a microservice with Spring Boot. We will create a couple of microservices and get them to talk to each other using Eureka Naming Server and Ribbon for Client Side Load Balancing. In part 1 of this series, lets get introduced to the concept of microservices and understand how to create great microservices with Spring Boot and Spring Cloud.

20+ Spring Boot Projects with Code Examples

At in28Minutes, we have created more than 20 projects with code examples on Github. We have 50+ articles explaining these projects. These code examples will you learn and gain expertise at Spring Boot.

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?

Index - 500+ Videos

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