Code Coverage
• Code coverage is a measure used to describe the degree to
which the source code of a program is tested by a particular
test suite.
• High code coverage means
ꟷ more thoroughly tested
ꟷhas a lower chance of containing software bugs
1
Coverage criteria
• Method coverage: Have all methods been called?
• Decision/Branch Coverage: Have all decisions been
executed in both the true and false paths?
• Condition Coverage: Have all conditionals been
executed in both the true and false paths?
• Statement coverage: Have all statements in a
method been executed?
2
Method Coverage
String getTriangle (int a, int b, int c) {
if (a == b && b == c) {
success();
return "Equilateral";
}
if (a == b || b == c || c == a) {
return "Isosceles";
}
void testCoverage()
return "Scalene"; {
} getTriangle(1,1,2);
getTriangle(2,2,2);
void success() }
{
printf(“Got it!”);
3
Condition Coverage
String getTriangle (int a, int b, int c) {
if (a == b && b == c) {
success();
return "Equilateral";
}
if (a == b || b == c || c == a) {
return "Isosceles";
}
void testCoverage()
return "Scalene"; {
} getTriangle(2,2,3);
getTriangle(2,2,2);
void success() getTriangle(1,2,2);
{ getTriangle(2,1,2);
printf(“Got it!”); }
}
4
Branch Coverage
String getTriangle (int a, int b, int c) {
if (a == b && b == c) {
success();
return "Equilateral";
}
if (a == b || b == c || c == a) {
return "Isosceles";
} void testCoverage()
{
return "Scalene";
getTriangle(2,2,3);
}
getTriangle(2,2,2);
void success() getTriangle(1,2,2);
{ getTriangle(2,1,2);
printf(“Got it!”); getTriangle(2,3,4);
}
}
5
Statement Coverage
String getTriangle (int a, int b, int c) {
if (a == b && b == c) {
success();
return "Equilateral";
}
if (a == b || b == c || c == a) {
return "Isosceles";
} void testCoverage()
{
return "Scalene";
getTriangle(2,2,3);
}
getTriangle(2,2,2);
void success() getTriangle(1,2,2);
{ getTriangle(2,1,2);
printf(“Got it!”); getTriangle(2,3,4);
}
}
6
Benefits and Limitations
• Benefits:
ꟷA measure of how complete your test cases are
ꟷHigh coverage does not guarantee code correctness!
ꟷCan identify paths through the code that you may have missed
• Limitations:
ꟷThe assumption that you are done testing if you have high
coverage is incorrect
ꟷCoverage tools only tell you if you’ve covered what’s there
ꟷThere may be requirements that you have missed!
ꟷDon’t write test cases ONLY to make your coverage tool happy
ꟷInspect surrounding code for other potential errors, perhaps
requirements you’ve missed
7
Code Coverage tools
• djUnit- http://works.dgic.co.jp/djunit/
ꟷEclipse plug‐in
ꟷMeasures
• Statement Coverage
• Branch Coverage
• EclEmma - http://eclemma.org/
ꟷEclipse plug‐in
ꟷMeasures (at the bytecode level)
• Instruction Coverage
• Block Coverage – roughly corresponds to condition coverage
• Line Coverage
• Method Coverage
• Type Coverage
• Clover -
https://www.atlassian.com/software/clover/overview
ꟷCommercial tool
• Method coverage
• Statement coverage
• Decision coverage
8