Code Coverage Tool: Statement, Branch & Decision Testing
โก Smart Summary
Code coverage is a white-box measurement that reports the degree to which source code has been exercised by a test suite, helping teams locate untested statements, branches, conditions, and paths that hidden defects may occupy.

What is Code Coverage?
Code coverage is a measure which describes the degree to which the source code of a program has been tested. It is one form of white box testing which finds the areas of a program not exercised by a set of test cases. It also helps create additional test cases to increase coverage and to determine a quantitative measure of code coverage.
In most cases, a code coverage system gathers information about the running program. It then combines that with source code information to generate a report about the test suite’s code coverage.
Why Use Code Coverage Testing?
Here are some prime reasons for using code coverage:
- It helps you measure the efficiency of test implementation.
- It offers a quantitative measurement of testing.
- It defines the degree to which the source code has been tested.
Code Coverage Methods
The following are the major code coverage methods:
- Statement Coverage
- Decision Coverage
- Branch Coverage
- Toggle Coverage
- FSM Coverage
Statement Coverage
Statement Coverage is a white box testing technique in which all the executable statements in the source code are executed at least once. It is used to calculate the number of statements in the source code that have been executed. The main purpose of Statement Coverage is to cover all the possible paths, lines, and statements in the source code.
Statement coverage is used to derive scenarios based upon the structure of the code under test.
In white box testing, the tester concentrates on how the software works. In other words, the tester concentrates on the internal working of the source code concerning control flow graphs or flow charts.
Generally in any software, if you look at the source code, there will be a wide variety of elements like operators, functions, looping, exception handlers, and so on. Based on the input to the program, some of the code statements may not be executed. The goal of Statement coverage is to cover all the possible paths, lines, and statements in the code.
Let us understand this with an example of how to calculate statement coverage. Here we take two different scenarios to check the percentage of statement coverage for each scenario.
Source Code:
Prints (int a, int b) { ------------ Printsum is a function int result = a + b; If (result > 0) Print ("Positive", result) Else Print ("Negative", result) } ----------- End of the source code
Scenario 1: If A = 3, B = 9
The statements marked in yellow are those which are executed as per the scenario. Number of executed statements = 5, Total number of statements = 7, so Statement Coverage = 5/7 = 71%.
Scenario 2: If A = -3, B = -9
The statements marked in yellow are those which are executed as per the scenario. Number of executed statements = 6, Total number of statements = 7, so Statement Coverage = 6/7 = 85%.
But overall, if you see, all the statements are covered by both scenarios. So we can conclude that the overall statement coverage is 100%.
What is covered by Statement Coverage?
- Unused Statements
- Dead Code
- Unused Branches
- Missing Statements
Decision Coverage Testing
Decision Coverage is a white box testing technique which reports the true or false outcomes of each boolean expression in the source code. The goal of decision coverage testing is to cover and validate all the accessible source code by checking and ensuring that each branch of every possible decision point is executed at least once.
In this coverage type, expressions can become complex, making it challenging to achieve 100% coverage. This is why various methods are used to report this metric. These methods prioritize the most critical combinations. While it is similar to branch coverage, it provides greater sensitivity to control flow.
Example of Decision Coverage
Consider the following code:
Demo(int a) { If (a > 5) a = a * 3 Print (a) }
Scenario 1: Value of a is 2. The “No” outcome of the decision If (a>5) is checked, so Decision Coverage = 50%.
Scenario 2: Value of a is 6. The “Yes” outcome of the decision If (a>5) is checked, so Decision Coverage = 50%.
| Test Case | Value of A | Output | Decision Coverage |
|---|---|---|---|
| 1 | 2 | 2 | 50% |
| 2 | 6 | 18 | 50% |
Branch Coverage Testing
Branch Coverage is a white box testing method in which every outcome from a code module (statement or loop) is tested. The purpose of branch coverage is to ensure that each decision condition from every branch is executed at least once. It helps measure fractions of independent code segments and find out sections that have no branches.
For example, if the outcomes are binary, you need to test both True and False outcomes.
The formula to calculate Branch Coverage:
Example of Branch Coverage
To learn branch coverage, consider the same example used earlier. Branch Coverage will consider the unconditional branch as well.
| Test Case | Value of A | Output | Decision Coverage | Branch Coverage |
|---|---|---|---|---|
| 1 | 2 | 2 | 50% | 33% |
| 2 | 6 | 18 | 50% | 67% |
Advantages of Branch coverage:
- Allows you to validate all the branches in the code.
- Helps you ensure that no branch leads to any abnormality in the program’s operation.
- Removes issues that happen because of statement coverage testing.
- Allows you to find areas which are not tested by other testing methods.
- Allows you to find a quantitative measure of code coverage.
- Branch coverage ignores branches inside boolean expressions.
Condition Coverage Testing
Condition Coverage, or expression coverage, is a testing method used to test and evaluate the variables or sub-expressions in a conditional statement. The goal of condition coverage is to check individual outcomes for each logical condition. Condition coverage offers better sensitivity to the control flow than decision coverage. In this coverage, only expressions with logical operands are considered.
For example, if an expression has boolean operations like AND, OR, or XOR, that indicates the total possibilities. Condition coverage does not guarantee full decision coverage.
The formula to calculate Condition Coverage:
For an expression with two operands, there are four possible combinations: TT, FF, TF, and FT. Consider the input X=3, Y=4 (x<y) TRUE and A=3, B=4 (a>b) FALSE, which gives Condition Coverage of 1/4 = 25%.
Finite State Machine Coverage
Finite state machine coverage is certainly the most complex type of code coverage method. This is because it works on the behavior of the design. In this coverage method, you need to look at how many times specific states are visited or transited. It also checks how many sequences are included in a finite state machine.
Which Type of Code Coverage to Choose
This is certainly the most difficult answer to give. In order to select a coverage method, the tester needs to check whether the:
- code under test has single or multiple undiscovered defects,
- cost of the potential penalty,
- cost of lost reputation,
- cost of lost sales, and so on.
The higher the probability that defects will cause costly production failures, the more severe the level of coverage you need to choose.
Code Coverage vs. Functional Coverage
| Code Coverage | Functional Coverage |
|---|---|
| Tells you how well the source code has been exercised by your test bench. | Measures how well the functionality of the design has been covered by your test bench. |
| Never uses a design specification. | Uses a design specification. |
| Done by developers. | Done by testers. |
Code Coverage Tools
Here is a list of important code coverage tools:
| Tool Name | Description |
|---|---|
| Cobertura | An open source code coverage tool. It measures test coverage by instrumenting a code base and analyzing which lines of code are executed and which are not when the test suite runs. |
| Clover | Clover (OpenClover) also reduces testing time by only running the tests which cover the application code modified since the previous build. |
| DevPartner | DevPartner enables developers to analyze Java code for code quality and complexity. |
| Emma | EMMA supports class, method, line, and basic block coverage, aggregated at source file, class, and method levels. |
| Kalistick | Kalistick is a third party application which analyzes the code from different perspectives. |
| CoView and CoAnt | A code coverage tool for metrics, mock object creation, code testability, path and branch coverage, and more. |
| Bullseye for C++ | BullseyeCoverage is a code coverage tool for C++ and C. |
| Sonar | Sonar is an open code coverage tool which helps you manage code quality. |
Advantages and Disadvantages of Using Code Coverage
| Advantages | Disadvantages |
|---|---|
| Helpful to evaluate a quantitative measure of code coverage. | Even when a specific feature is not implemented in the design, code coverage still reports 100% coverage. |
| Allows you to create extra test cases to increase coverage. | It is not possible to determine whether all possible values of a feature were tested using code coverage. |
| Allows you to find the areas of a program not exercised by a set of test cases. | Code coverage does not tell how much and how well you have covered your logic. |






