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