What is Code Coverage?
code coverage is done by developers to measures the degree to which the coding of the application or component has been tested, shows the information of the running program, helps to measure the test efficiency. Statement coverage, toggle coverage, branch coverage, decision coverage, and FSM coverage are the methods used by the code coverage. It also allows developers to create extra test case to increase the code coverage.
How does Code Coverage work?
There actually exist various approaches to the measurement of code coverage. However, if we consider in a broad way, there exist 3 of the approaches that are as follows:
- Source Code Instrumentation: Source code instrumentation will add the statements of instrumentation to the source code. Then, it will compile the code along with the normal compile tool to get us an assembly that is instrumented.
- Intermediate Code Instrumentation: In Intermediate code instrumentation, compiled files get instrumented through the addition of the bytecodes that are new, and after that, the new class gets generated.
- Runtime Information Collection: In Runtime information collection, we gather the information from the environment that is runtime since code is executed so as to determine the coverage information.
Since the code gets executed under the test, the code coverage system would be collecting information as to which of the statements are executed. Now, this information would be used as some of the basis of the reports. Additionally, code coverage methods differ depending on the various forms of the coverage information that is being collected. There exist various methods of code coverage if we go beyond the basic statement coverage methods.
Methods of Code Coverage
There exist many methods of code coverage. A few of them are listed below:
- Condition Coverage
- FSM Coverage
- Statement Coverage
- Decision Coverage
- Branch Coverage
1. Statement Coverage
- It is the white box test design technique wherein we deal with the execution of all of the executable statements at least once that exists in the source code. Therefore, code coverage is needed in the calculation and the measurement of many statements that exist in source code and those that can get executed provided that the requirements are given.
- This method is needed in deriving the scenarios depending on the code structure that exists under the test.
2. Decision Coverage
- This method is useful in reporting either true or false outcomes of every expression that is Boolean. But, unfortunately, the expressions in this coverage often become complicated. That is why it is quite difficult to get 100% coverage.
- That is the very reason that there exist various methods to report this kind of metric. These entire methods basically deal with the coverage of the most significant combinations. Decision Coverage provides great sensitivity so as to control the flow.
3. Branch Coverage
- When we talk about this type, we mean that each output that comes from the code module gets tested. So, as an example, we can consider that if the outputs are binary, then we will be testing True and False outputs.
- Branch Coverage helps us in ensuring that all the possible branches that come from each of the decisions, the condition gets executed a minimum one time.
- Through the Branch coverage method, we can calculate the fraction of the code segments that are independent. In addition, this method enables us to find out what sections of the code don’t have branches.
There exists the formula of the calculation of Branch Coverage:
Branch Coverage = Number of Executed Branches / Total No of Executed Branches

4.5 (8,552 ratings)
View Course
4. Condition Coverage
- Condition coverage, that is also called as expression coverage tells us as to how do the variables or the subexpressions that exist in conditional statement gets evaluated.
- Let us consider an example wherein an expression consists of the Boolean operations such as AND, OR or XOR that consists of the total number of possibilities.
- This method actually gives us much better sensitivity for controlling the flow as compared to the decision coverage. This method doesn’t give us a guarantee regarding the full decision coverage.
There exists the formula of the calculation of Branch Coverage:
Condition Coverage = Number of Executed Operands / Total No of Executed Operands
5. Finite State Machine Coverage
- This method is considered as the most complex kind of code coverage method. The reason behind it is that this method actually works on the behavior of the design.
- Also, in this method, we got to find out how many of the time-specific states get visited and transited. In addition, this method too verifies how many of the sequences get considered in the finite state machine.
Advantages of Code Coverage
Some of the advantages are given below:
- It is quite useful in the evaluation of the code coverage’s quantitative measure.
- It also permits us to create the test cases that are extra to increase the coverage.
- It also permits us to locate those parts of the program that are not exercised by the test cases.
Disadvantages of Code Coverage
Some of the disadvantages are given below:
- Whenever a particular feature isn’t well implemented inside the design, the code coverage would still be reporting coverage is 100%.
- It’s impossible to understand if you test all of the possible values of the feature through this.
- It also doesn’t tell as to how much and as to how well we covered our logic.
Conclusion
The code coverage is considered the measure wherein we describe the degree to which the source code of a program has been tested. It also helps us to calculate test implementation’s efficiency. Also, statement coverage means executing all of the executable statements minimum of one time.
Recommended Articles
This is a guide to Code Coverage. Here we discuss the methods and how does it work? along with advantages and disadvantages. You may also look at the following articles to learn more –