Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested. It is a form of testing that looks at the code directly and as such comes under the heading of white box testing. Currently, the use of code coverage is extended to the field of digital hardware, the contemporary design methododology of which relies on Hardware description languages (HDL's). Code coverage techniques were amongst the first techniques invented for systematic software testing. The first published reference was by Miller and Maloney in Communications of the ACM in 1963.
Contents |
Coverage criteria
To measure how well the program is exercised by a test suite, one or more coverage criteria are used. There are a number of coverage criteria, the main ones being:
- Function coverage - Has each function in the program been executed?
- Statement coverage - Has each line of the source code been executed?
- Condition coverage - Has each evaluation point (such as a true/false decision) been executed?
- Path coverage - Has every possible route through a given part of the code been executed?
- Entry/exit coverage - Has every possible call and return of the function been executed?
Safety-critical applications are often required to demonstrate that testing achieves 100% of some form of code coverage. Some of the coverage criteria above are connected. For instance, path coverage implies condition, statement and entry/exit coverage. Statement coverage does not imply condition coverage, as the code (in the C programming language) below shows:
void foo(int bar)
{
printf("This is ");
if (bar <= 0)
{
printf("not ");
}
printf("a positive integer.\n");
return;
}
If the function foo were called with variable bar set to −1, statement coverage would be achieved. Condition coverage, however, would not. Full path coverage, of the type described above, is usually impractical or impossible. Any module with a succession of <math>n</math> decisions in it can have up to <math>2^n</math> paths within it; loop constructs can result in an infinite number of paths. Many paths may also be infeasible, in that there is no input to the program under test that can cause that particular path to be executed. However, a general-purpose algorithm for identifying infeasible paths has been proven to be impossible (such an algorithm could be used to solve the halting problem). Techniques for practical path coverage testing instead attempt to identify classes of code paths that differ only in the number of loop executions, and to achieve "basis path" coverage the tester must cover all the path classes.
Code coverage in practice
Usually the source code is instrumented and run through a series of tests. The resulting output is then analysed to see what areas of code have not been exercised, and the tests are updated to include these areas as necessary. Combined with other code coverage methods the aim is to develop a rigorous yet manageable set of regression tests. Code coverage is ultimately expressed as a percentage, as in "We have tested 67% of the code." The meaning of this depends on what form(s) of code coverage have been used, as 67% path coverage is more comprehensive than 67% statement coverage. The value of code coverage as a measure of test quality is debated (see external links).
See also
- Mutation testing
- Software metric
- Regression testing
- Static code analysis
- White box testing
- Panopticode - An open source project that provides treemap visualization of code coverage in Java projects
External links
General
- Code Coverage Analysis
- Introduction to Code Coverage
- What lies beneath -- Discovering untested code
- Don't be fooled by the coverage report
- Lessons in achieving 100% code coverage
Tools
- Semantic Designs Test Coverage Tools for C, C++, C#, COBOL, Java, PHP
- Visual Studio Team System for Software Developers supports code coverage for both .NET and C/C++
- IBM Rational PureCoverage, packaged with Rational PurifyPlus
- IBM Rational Application Developer can profile Java application for Code Coverage analysis
- IBM Rational Test RealTime can profile embedded software for Code Coverage analysis and more
- LDRA Testbed measures statement coverage, branch/decision coverage, LCSAJ Coverage, procedure/function call coverage, branch condition coverage, branch condition combination coverage and modified condition decision coverage (MC/DC) for D0-178B Level A.
- Range of Code Coverage Tools for C & Java
- CodePro AnalytiX: Code coverage tool for Eclipse and Java
- gcov - a GNU Test Coverage Program
- D programming language code coverage analysis tool
- EMMA, a free tool for Java code coverage
- Netbeans Code Coverage Plugin: emma-based coverage plugin for Netbeans the visualizes unit tests coverage by coloring sources
- Eclipse EMMA plugin
- NCover, a tool for testing code coverage on the .NET runtime
- trace2html: a Python coverage test reporting tool with black and whitelisting support
- coverage.py: another coverage reporter for Python
- Clover: Code coverage tool for Java
- CodeCover: Extensible code coverage tool supporting Java and COBOL Supports statement coverage, branch coverage, loop coverage and strict condition coverage, fully integrated into Eclipse and licensed under the EPL.
- Cobertura: Code coverage tool for Java
- CoverageMeter: Code coverage tool for C/C++
- Testwell CTC++: Test Coverage Analyzer for C/C++
- PartCover: Another tool for testing code coverage on the .NET runtime
- VB Watch Profiler: Code coverage tool for Visual Basic
- Dynamic Code Coverage: Code coverage tool for Unix C/C++
- Devel::Cover: Code coverage tool for Perl 5


