Test coverage is a measure of the amount of source code executed by the test cases. It is an important metric to determine whether the unit tests are sufficiently thorough for the code. Test coverage closely relates to the cyclomatic complexity of the program. A program with higher cyclomatic complexity requires more extensive test cases to cover all possible execution paths adequately.
A higher test coverage indicates more thorough testing and thus correlates with reduced occurrence of bugs and faults in production. Most codebases employ automation to ensure that coverage does not go down with any change.
What are common coverage types?
- Function coverage: This coverage measures the number of tested functions, methods, and subroutines as a ratio against the total number of such subprograms.
- LoC coverage: This coverage measures the number of lines of code tested as a ratio against the total number of lines in the codebase.
- Branch coverage: This coverage ensures that test cases cover code in all possible branches of conditional statements such as if-else and switch-case.
Write clean and secure code with DeepSource
Powerful static analysis that takes 5 minutes to set up and helps you fix code health and security problems on every pull request.