All About Code Coverage – A White Box Testing Technique

Let’s first try to understand what Code coverage is.

It is a kind of metric intended to measure the test effort applied to the software application.

It is aimed at directly inspecting the code and is therefore a form of white box testing.

How do we record code coverage statistics?

The process includes instrumentation of the program and execution of the tests. This way we can identify the code that has been executed and that has been omitted. We can see that unit testing and code coverage complement each other. Unit tests confirm the conformity of the program performance to the requirements, while code coverage reveals the areas left out of the tests.

The software development process aims to have coverage measurement by defining the number of branches or statements covered by the test. Even after full branch coverage or instruction coverage, there is no assurance that some important bugs are missing from the code. Therefore, 100% branch coverage or statement coverage remains quite elusive and does not provide any perfection for both developers and managers

The main point of discussion now remains that full coverage is insufficient. The reason is that both branch coverage and statement coverage do not confirm the implementation of the logic of the code. Both branch coverage and statement coverage are helpful in identifying major issues in a portion of the code that is not running.

However, the pad coverage technique is relatively much more robust and helps us reveal the defects in the early stages. Before going deeper into path coverage, let’s analyze some of the drawbacks of industry coverage and statement coverage techniques.

Cover statement:

The main advantage of the coverage of instructions is that it is able to isolate the part of the code that could not be executed. Statement coverage criteria require a sufficient number of test cases for the program to ensure that each statement is run at least once. Despite achieving 100% statement coverage, chances are there are a lot of unnoticed bugs.

The coverage report indicating 100% coverage will trick the manager into feeling happy with a false temptation to end further testing, which could lead to the release of a faulty mass production code. So we can’t see 100% statement coverage enough to build a reasonable degree of confidence on the perfect behavior of the application.

Since coverage of 100% of the statements often gets expensive, the developers chose a better testing technique called industry coverage.

Business coverage or decision coverage:

Industry coverage is more impressive because it tends to go deeper into the code compared to the statement coverage technique. Establishment coverage is a measure of measuring the results of decisions being tested. Industry coverage criteria require that there is an adequate number of test cases for each program to ensure that every decision or industry is executed at least once. In most products, industry coverage is considered the absolute minimum coverage. So this is better than statement coverage, but still not suitable for applications that require higher integrity.

It is easy to determine how many branches a method will have. A simple method of working out the decision results, which one method may have, is to count the amount of branches that are expected to be covered, along with adding another entry branch therein.

It has been found that even reaching 100% of coverage and of the sites is not enough. Moreover, it is practically not feasible for complex methods to test every path. This requires a better alternative to testing, namely basic path coverage.

Basic path coverage:

A path is nothing but the execution of a method from start to finish. Paths, which are independent, are also known as basic sets. In other words, the base set is the smallest group of paths grouped together to form all possible combinations of paths in the entire method.

Path coverage criteria require a sufficient number of test cases for each feasible path, base paths to ensure that each path is executed at least once in the program segment.

If there are a number of decisions in a method, it can have 2 ^ N number of paths. However, if a method has a loop, the number of paths can become infinite. Due to the extremely large number of paths in the application, path coverage also often becomes difficult. To reduce the number of test paths, we can use the help of the metric known as Cyclomatic Complexity. The question of how much path coverage is needed is answered depending on the complexity or risk factor involved in the application under test.

Basic path coverage is similar to industry coverage in a way that guarantees the testing / execution of each decision result. Still, there is a point of difference from industry coverage in such a way that each decision outcome is tested independently. It is about reversing the decision made just before, while leaving the remaining branches out intact. Since basic path coverage takes care of all statements and sites about a method, this is an effective replacement for site coverage and statement coverage. This is the reason that base path coverage is considered much more robust compared to branch coverage.

Create data for testing:

In simple examples, it may be easy to achieve full base path coverage, while in real world scenarios, full base path testing becomes extremely challenging, perhaps even impossible. One of the reasons for this is the need for test data, which must be able to run a given path by testing the interaction between different decisions about a method. It is not a simple process to enter data that can further lead to the execution of a particular path.

However, following best coding practices can be helpful to simplify the testing process.

1) Keep a simple code.

2) Do not attempt to use methods with a cyclomatic complexity greater than ten.

3) Do not have more basic paths in number.

4) Make fewer decisions for each path.

5) Don’t have double decisions.

6) Try not to have data dependency


The coverage of the statements and the criteria for coverage of the establishments, while simple to implement, have the drawback that some of the major flaws go undetected, leading project managers, developers and testers to draw whimsical conclusions of perfection while the fact is slightly different.

While basic path coverage is a more robust approach capable of identifying such defects that could otherwise have gone unnoticed.

Source by Yogindernath Gupta