In computer science, code coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. A program with high code coverage, measured as a percentage, has had more of its source code executed during testing which suggests it has a lower chance of containing undetected software bugs compared to a program with low code coverage. Many different metrics can be used to calculate code coverage; some of the most basic are the percentage of program subroutines and the percentage of program statements called during execution of the test suite.
Code coverage was among the first methods invented for systematic software testing. The first published reference was by Miller and Maloney in Communications of the ACM in 1963.
To measure what percentage of code has been exercised by a test suite, one or more coverage criteria are used. Coverage criteria is usually defined as a rule or requirement, which test suite needs to satisfy.
There are a number of coverage criteria, the main ones being:
For example, consider the following C function:
Assume this function is a part of some bigger program and this program was run with some test suite.
Condition coverage does not necessarily imply branch coverage. For example, consider the following fragment of code:
Condition coverage can be satisfied by two tests:
However, this set of tests does not satisfy branch coverage since neither case will meet the if
condition.
Fault injection may be necessary to ensure that all conditions and branches of exception handling code have adequate coverage during testing.
A combination of function coverage and branch coverage is sometimes also called decision coverage. This criterion requires that every point of entry and exit in the program has been invoked at least once, and every decision in the program has taken on all possible outcomes at least once. In this context the decision is a boolean expression composed of conditions and zero or more boolean operators. This definition is not the same as branch coverage, however, some do use the term decision coverage as a synonym for branch coverage.