The ABCs of Code Coverage (2024)

Society is increasingly dependent on technology. Software-driven devices such as automatic door openers, keyless doors, coffee makers, Siri, and Alexa are in schools, hospitals, retail outlets, and homes. Companies and developers who create the software that controls these smart devices need to provide a bug-free product. Software developers and testers need to make sure that the test-scripts cover all parts of the code. Code coverage is a way to check that test scripts test all the modules of the code.

What is Code Coverage?

Code coverage is often confused with test coverage. Test coverage is the measure or the percentage of testing done on the code, whereas code coverage is the percentage of code covered during manual and automation testing. Although 100% code coverage is possible, it may be hard to achieve. A more realistic goal for code coverage is 70 – 95%. Code coverage falls into the category of white-box testing. White-box testing is where the code is visible to the tester. White-box testing helps developers and testers to write more efficient test scripts and suites.

Types of Code Coverage

Statement Coverage

Statement coverage is the simplest of the test coverages. It requires tests to execute all statements in the code at least once. Developers and testers often write multiple test scripts to meet the code coverage goal.

Coverage

Branch coverage is sometimes mistakenly called decision coverage. Although they are related, they have different definitions. Decision coverage is a subset of branch coverage and is the measurement of the conditional branches in the code. Branch coverage is a measure of both the conditional branches and the unconditional branches of code.

Path Coverage

Path coverage tests all paths in the code. A code path is the execution of the module’s entry point to the exit point. Path coverage is more complicated than statement and branch coverage because the code may contain an unlimited number of paths.

How to Calculate Code Coverage

Below is a Java method to calculate the number of pairs of socks created from single socks. This code will serve as an example to calculate the statement, branch, and path coverage.

The ABCs of Code Coverage (1)

The test cases to the method are:

Test Case 1: array = {10, 20, 8, 9}

Test Case 2: array = {10, 20, 20, 10,30 ,50 ,10 ,20}

Statement Coverage

The formula for calculating the statement coverage percentage is:

Statement Coverage = number of executed statementstotal / number of statements

Test Case 1

Statements 1, 2, 4, 5, 10, 11, 12, 13, 14, 15, 16, 18, 19 and 20 are executed. The number of executed statements is 14. The total number of statements is 18.

The statement coverage percentage for Test Case 1 is 77.78%.

Test Case 2

Statements 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19 and 20 are executed. The number of executed statements is 18. The total number of statements is 18.

The statement coverage percentage for Test Case 2 is 100%.

Branch Coverage

The formula for calculating the statement branch coverage percentage is:

Branch Coverage = number of executed branches / total number of branches

There is one if-statement. The if-statement has a branch for each of the test conditions. Therefore, the branch total is two.

Test Case 1

One branch, the false branch, is executed. Therefore, the branch coverage is 50%.

Test Case 2

Both branches execute within the for-loop. Therefore, the branch coverage is 100%.

Path Coverage

The number of paths in the code can be unlimited. It is best to test the path code with test data that gives the optimal coverage. In the above example, Test Case 2 allows for optimal testing of the code. The three basic paths discovered using Test Case 2 results in 100% path coverage.

What Coverage Should I use?

There are no set guidelines on which code coverage to use. Developers and testers can use a combination of code coverage techniques to reach the coverage goal.

References

Banner Image – Image by Elchinator from Pixabay

Code example – An algorithm problem from HackRank was adapted to create the code example used in this article. HackerRank. “Sales by Match.” HackerRank, 2016, www.hackerrank.com/challenges/sock-merchant/problem .

The ABCs of Code Coverage (2024)
Top Articles
Pool Solar Covers Frequently Asked Questions
Escalation Management [Defined and Explained] - Simplr
Cappacuolo Pronunciation
Chambersburg star athlete JJ Kelly makes his college decision, and he’s going DI
Frank Lloyd Wright, born 150 years ago, still fascinates
Here are all the MTV VMA winners, even the awards they announced during the ads
Dr Doe's Chemistry Quiz Answer Key
Gunshots, panic and then fury - BBC correspondent's account of Trump shooting
Fnv Turbo
Walgreens Alma School And Dynamite
Barstool Sports Gif
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Tight Tiny Teen Scouts 5
U Arizona Phonebook
Walmart Car Department Phone Number
Promiseb Discontinued
Iu Spring Break 2024
Atdhe Net
Football - 2024/2025 Women’s Super League: Preview, schedule and how to watch
Panolian Batesville Ms Obituaries 2022
Lexus Credit Card Login
Craig Woolard Net Worth
Kohls Lufkin Tx
Makemv Splunk
fft - Fast Fourier transform
Timeline of the September 11 Attacks
Dashboard Unt
Tim Steele Taylorsville Nc
Vlacs Maestro Login
Purdue Timeforge
Frequently Asked Questions - Hy-Vee PERKS
Ancestors The Humankind Odyssey Wikia
Emily Katherine Correro
Urban Blight Crossword Clue
Where Can I Cash A Huntington National Bank Check
Bratislava | Location, Map, History, Culture, & Facts
What Time Does Walmart Auto Center Open
Vision Source: Premier Network of Independent Optometrists
Academy Sports New Bern Nc Coupons
Seven Rotten Tomatoes
Quaally.shop
Holzer Athena Portal
Swsnj Warehousing Inc
Samsung 9C8
Race Deepwoken
Craigslist Anc Ak
Slug Menace Rs3
Cvs Minute Clinic Women's Services
Sleep Outfitters Springhurst
Okta Hendrick Login
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 6247

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.