What is code coverage?
Code coverage is a measurement of the proportion of code that is executed while automated tests are being run. Code coverage helps in determining the quality of code and investigating how thoroughly a software product has been verified.
A program with a high level of code coverage means there will be a lower probability of finding bugs compared to a program with low code coverage, simply because high code coverage means more of the code has been tested.
The basic criteria for code coverage covers the following aspects:
- Statement coverage
- Function coverage
- Branch coverage
- Condition coverage
- Edge coverage
Statement coverage
Statement coverage refers to the number of statements or lines that have been successfully executed in the source code.
Function coverage
Function coverage refers to the number of functions and subroutines that are called at least once in the execution of the source code.
Branch coverage
Branch coverage checks whether each branch of every control structure has been successfully executed or not.
Condition coverage
Condition coverage checks if each boolean expression has been successfully executed to provide both true and false results.
Edge coverage
Edge coverage determines if all edges in a control-flow graph have been executed.
Advantages
Code coverage helps in quantitatively assessing the quality of the code and makes it easy to maintain the codebase. More importantly, it helps to identify and expose bad chunks of code that may be error-prone because of inadequate testing. Code coverage is also a good measure of how thorough and reliable the testing suite is; it can also help discover new test cases.
Disadvantages
It is impossible to determine how perfectly the code is covered. Code coverage does not guarantee that all possible test cases or values will be tested.
Tools
There are many open-source and proprietary software available for use in testing code coverage. Some of the more popular tools are:
- Cobertura
- Clover
- Gretel
- Kalistick
- JaCoCo
- JTest
- OpenCover
- Emma
- GCT
Free Resources