Written by Member
Software Testing Techniques
What is Code Coverage?
Code coverage measurement simply determines those statements in a body of code which have been executed through a test run and those which have not. In general, a code coverage system collects information about the running program and then combines that with source information to generate a report on test suite's code coverage.
Code coverage is part of a feedback loop in the development process. As tests are developed, code coverage highlights aspects of the code which may not be adequately tested and which require additional testing. This loop will continue until coverage meets some specified target.
Why Measure Code Coverage?
It is well understood that unit testing improves the quality and predictability of your software releases. However, how well your unit tests actually test your code? How many tests are enough? Do you need more tests? These are the questions code coverage measurement seeks to answer.
Coverage measurement also helps to avoid test entropy. As your code goes through multiple release cycles, there can be a tendency for unit tests to atrophy. As new code is added, it may not meet the same testing standards you put in place when the project was first released. Measuring code coverage can keep your testing up to the standards you require. You can be confident that when you go into production there will be minimal problems because you know the code not only passes its tests but that it is well tested.
In summary, we measure code coverage for the following reasons:
* To know how well our tests actually test our code
* To know whether we have enough testing in place
* To maintain the test quality over the lifecycle of a project
Code coverage is not a panacea. Coverage generally follows an 80-20 rule. Increasing coverage values becomes difficult with new tests delivering less and less incrementally. If you follow defensive programming principles where failure conditions are often checked at many levels in your software, some code can be very difficult to reach with practical levels of testing. Coverage measurement is not a replacement for good code review and good programming practices.
In general you should adopt a sensible coverage target and aim for even coverage across all of the modules that make up your code. Relying on a single overall coverage figure can hide large gaps in coverage.
How Code Coverage Works
There are many approaches to code coverage measurement. Broadly there are three approaches, which may be used in combination:
Source Code Instrumentation:
This approach adds instrumentation statements to the source code and compiles the code with the normal compile tool chain to produce an instrumented assembly.
Intermediate code Instrumentation:
Here the compiled class files are instrumented by adding new bytecodes and a new instrumented class generated.
Runtime Information collection
: This approach collects information from the runtime environment as the code executes to determine coverage information
The Total Coverage Percentage allows entities to be ranked in reports. The Total Coverage Percentage (TPC) is calculated as follows:
TPC = (BT + BF + SC + MC)/(2*B + S + M)
where
BT - branches that evaluated to "true" at least once
BF - branches that evaluated to "false" at least once
SC - statements covered
MC - methods entered
B - total number of branches
S - total number of statements
M - total number of methods
Also check the following topics:
Code Coverage Tools
Java Code Coverage
Path Coverage
Decision Coverage
Statement Coverage
C++ Code Coverage
Code Coverage Criteria
Code Coverage Matrices