What is Code Coverage? | Atlassian (2024)

Code coverage is a metric that can help you understand how much of your source is tested. It's a very useful metric that can help you assess the quality of your test suite, and we will see here how you can get started with your projects.

How Code Coverage is Calculated

Code coverage tools will use one or more criteria to determine how your code was exercised or not during the execution of your test suite. The common metricsthat you might see mentioned in your coverage reports include:

  • Function coverage:how many of the functions defined have been called.
  • Statement coverage:how many of the statements in the program have been executed.
  • Branches coverage:how many of the branches of the control structures (ifstatements for instance) have been executed.
  • Condition coverage:how many of the boolean sub-expressionshave been tested for a true and a false value.
  • Line coverage:how many of lines of source code have been tested.

These metrics are usually represented as the number of items actually tested, the items found in your code, and a coverage percentage (items tested / items found).

These metrics are related, but distinct. In the trivial script below, we have a Javascript function checking whether or not an argument is a multiple of 10. We'll use thatfunctionlater to check whether or not 100 is a multiple of 10. It'll help understand the difference between thefunction coverageandbranch coverage.

Related material

Automated testing for DevOps

Read more

coverage-tutorial.js

functionisMultipleOf10(x){if(x%10==0)returntrue;elsereturnfalse;}console.log(isMultipleOf10(100));


We can use the coverage toolistanbulto see how much of our code is executed when we run this script. After running the coverage tool we get a coverage report showing our coverage metrics. We can see that while ourFunctionCoverageis 100%, ourBranch Coverageis only 50%. We can also see that the isntanbul code coverage tool isn't calculating aCondition Coveragemetric.

What is Code Coverage? | Atlassian (1)

This is because when we run our script, theelsestatement has not been executed. If we wanted to get 100% coverage, we could simply add another line, essentially another test, to make sure that all branches of the if statement is used.

coverage-tutorial.js

functionisMultipleOf10(x){if(x%10==0)returntrue;elsereturnfalse;}console.log(isMultipleOf10(100));console.log(isMultipleOf10(34));//Thiswillmakeourcodeexecutethe"returnfalse;"statement.


A second run of our coverage tool will now show that 100% of the source is covered thanks to our two console.log() statements at the bottom.

What is Code Coverage? | Atlassian (2)

In this example, we were just logging results in the terminal but the same principal applies when you run your test suite. Your code coverage tool will monitor the execution of your test suite and tell you how much of the statements, branches, functions and lines were run as part of your tests.

What is Code Coverage? | Atlassian (3)

Code Coverage: 6 Tips to Get Started

1. Find the right tool for your project

You might find several options to create coverage reports depending on the language(s) you use. Some of the popular tools are listed below:

A comparison of tools can also help you make your decision. Some tools likeistanbulwill output the results straight into your terminal while others can generate a full HTML report that lets you explore which part of the code are lacking coverage.

2. What percentage of coverage should you aim for?

There's no silver bullet in code coverage, and a high percentage of coverage could still be problematic if critical parts of the application are not being tested, or if the existing tests are not robust enough to properly capture failures upfront. With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit.

The first time you run your coverage tool you might find that you have a fairly low percentage of coverage. If you're just getting started with testing it's a normal situation to be in and you shouldn't feel the pressure to reach 80% coverage right away. The reason is that rushing into a coverage goal might push your team to write tests that are hitting every line of the code instead of writing tests that are based on the business requirements of your application.

For instance, in the example above we reached 100% coverage by testing if 100 and 34 were multiples of 10. But what if we called our function with a letter instead of a number? Should we get a true/false result? Or should we get an exception? It is important that you give time to your team to think about testing from a user perspective and not just by looking at lines of code. Code coverage will not tell you if you're missing things in your source.

3. Focus on unit testing first

Unit tests consist in making sure that the individual methods of the classes and components used by your application are working. They're generally cheap to implement and fast to run and give you an overall assurance that the basis of the platform is solid. A simple way to increase quickly your code coverage is to start by adding unit tests as, by definition, they should help you make sure that your test suite is reaching all lines of code.

4. Use coverage reports to identify critical misses in testing

Soon you'll have so many tests in your code that it will be impossible for you to know what part of the application is checked during the execution of your test suite. You'll know what breaks when you get a red build, but it'll be hard for you to understand what components have passed the tests.

This is where the coverage reports can provide actionable guidance for your team. Most tools will allow you to dig into the coverage reports to see the actual items that weren't covered by tests and then use that to identify critical parts of your application that still need to be tested.

What is Code Coverage? | Atlassian (4)

5. Make code coverage part of your continuous integration flow when you're ready

When you've established your continuous integration (CI) workflow you can start failing the tests if you don't reach a high enough percentage of coverage. Of course, as we said it earlier, it would be unreasonable to set the failure threshold too high, and 90% coverage is likely to cause your build to fail a lot. If your goal is 80% coverage, you might consider setting a failure threshold at 70% as a safety net for your CI culture.

Once again, be careful to avoid sending the wrong message as pressuring your team to reach good coverage might lead to bad testing practices.

6. Good coverage does not equal good tests

Getting a great testing culture starts by getting your team to understand how the application is supposed to behave when someone uses it properly, but also when someone tries to break it. Code coverage tools can help you understand where you should focus your attention next, but they won't tell you if your existing tests are robust enough for unexpected behaviors.

Achieving great coverage is an excellent goal, but it should be paired with having a robust test suite that can ensure that individual classes are not broken as well as verify the integrity of the system.

You can learn more here aboutthe different types of software testing. Atlassian’s Open DevOps provides an open toolchain platform that allows you to build a CD-based development pipeline with the tools you love. Learn how Atlassian and third-party tools can integrate testing in your workflow with our DevOps testing tutorials.

What is Code Coverage? | Atlassian (5)

Sten Pittet

I've been in the software business for 10 years now in various roles from development to product management. After spending the last 5 years in Atlassian working on Developer Tools I now write about building software. Outside of work I'm sharpening my fathering skills with a wonderful toddler.

Share this article

Next Topic
The different types of testing in software
What is Code Coverage? | Atlassian (2024)
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6463

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.