What Is Code Coverage?

Code coverage ensures your software gets thoroughly tested, improving the speed and quality of the product you deliver to customers.
A woman on a laptop with a zoomed in task list in the background

When architects design a building, they don't just test one wall, one pillar, or one section of the roof—they check everything. That's why code coverage is so important to the software development process. It ensures that code is thoroughly tested before being released to customers.

Decoding Code Coverage: What Exactly Is It?

In this fast-paced world of software development, scrum teams are expected to rapidly deliver working software, ideally every sprint (and in some cases, multiple times during a sprint). In fact, Amazon releases code 136,000 times a day! The practice of code coverage supports faster speed-to-market with a higher-quality product.

Scrum Alliance defines code coverage as the extent to which a program's source code is executed during tests, helping measure test thoroughness.

This testing metric decides the number of lines of code that are validated under a test procedure, which helps analyze how comprehensively the software is verified. 

To calculate the code coverage percentage, simply use the following formula:

Code Coverage Percentage = (Number of lines of code executed by testing over the total number of lines of code) x 100.

There are 5 key things that code coverage tests. Those are:

  1. Function Coverage. Ensuring that each function in the source code is called and run at least one time.
  2. Statement Coverage. The count of individual statements in the code that have been tested and confirmed to work.
  3. Path Coverage. The various sequences of execution paths, including all control flows and conditions, that have been successfully tested at least once.
  4. Branch or Decision Coverage. The evaluation of decision-making structures, such as loops, to ensure they have been executed correctly.
  5. Condition Coverage. Testing Boolean expressions that are validated and that executes to both TRUE and FALSE during the test scenarios.

By thorough testing, the team can make sure that the software meets the five quality metrics:

  1. Correctness
  2. Reliability
  3. Effectiveness
  4. Security
  5. Maintainability

Code coverage is a great practice for your agile team to incorporate into its ways of working in order to easily maintain its code base, quickly expose bad code, and improve speed-to-market.

Understanding Code Coverage Standards

If you have 100 lines of code and all 100 lines are tested, then you have 100 percent code coverage. Most teams aim to be somewhere in the 80 percent range.

According to Codegrip, "Don't focus on achieving 100 percent coverage. The analysis should be clubbed with scalable, robust test scripts, covering every functional and non-functional area of the source code."

Code coverage can help your team spot untested or poorly tested code, detect bugs and vulnerabilities and improve maintainability and reliability and allow code to be scaled.

This practice transparently exposes bad code, allowing the team to inspect and adapt how it works to improve code quality over time.

It's important for your team to set its coverage goals and to have shared accountability that the goals are being met and that results are accurately interpreted. There are several tools that can enable code coverage for your software and assist your team in analyzing results. 

Here are a few examples:

  • Cobertura
  • Clover
  • Gretel
  • Kalistick
  • JaCoCo
  • JTest
  • OpenCover
  • Emma
  • GCT

Even 100 percent code coverage doesn't guarantee bug-free code, so your team should never make that assumption. However, it does help look for dead code and can help with the overall cleanup and maintenance of better-quality code over time.

With the help of code coverage metrics, scrum teams can finish the software development process faster, allowing companies to launch more software, leading to improved customer satisfaction and better ROI.

Try Code Coverage on Your Agile Team

While code coverage is not part of agile or the scrum framework, it enables the agile values and principles of delivering working software quickly and continuous improvement. Code coverage can help make your software more stable and reliable over time. While 100 percent coverage isn't the goal, strive for somewhere around 80 percent and keep focusing on writing quality code to begin with.

If you're a software developer or in a related role and want to learn more about leveraging iterative, incremental work practices that deliver value reguarly, check out the Scrum Alliance Certified Scrum Developer® (CSD®) course.

Learn more about CSD

RL_570_code-coverage
Stay Connected

Get the latest resources from Scrum Alliance delivered straight to your inbox

Subscribe