Code coverage metrics

Branch coverage

Statement coverage

Method coverage

Total Coverage Percentage (Coverage %, TPC)

The Total Coverage Percentage is calculated as follows:

TPC = (BT + BF + SC + MC)/(2*B + S + M) * 100%
 
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

 

Code complexity metrics

Average Method Complexity (Avg Method Cmp)

The average method complexity of code in the given context. In case of:

Complexity (Cmp, Total Cmp)

Cyclomatic complexity of code in the given context.

Method Complexity

Cyclomatic complexity of a single method. It's calculated as follows:

Complexity Density (Cmp Density)

Complexity Density is the average number of paths in your code per statement in given context (method, class, package). It's calculated as follows:

Cmp Density = Complexity / number of statements

LOC

Lines Of Code (including comment lines).

NC LOC

Non-Commented Lines Of Code. Lines of code that contain comments are not counted in this metric, leaving only the actual functioning code itself.

Examples

// # of statements, # of branches, Method Cyclomatic Complexity

// 0, 0, 1
void A() {}

// 1, 0, 1
void A() { a(); }

// 1 ,0 ,1
void A() { a = (6 < 7); }

// 3, 0, 1
void A() { a(); b(); c(); }

// 3, 2, 2
void A() { if (a()) b(); else c(); }

// 2, 2, 3
void A() { if (a() || b()) c(); }

// 2, 0, 1
void A() { if (1 + 2 == 4) c(); }

// 2, 2, 2
void A() { for (; a(); ) b(); }

// 2, 2, 3
void A() { for (; a() || b(); ) c(); }

// 2, 0, 1
void A() { for (; 1 + 2 == 4; ) c(); }

// 2, 2, 2
void A() { while (a()) b(); }

// 2, 2, 3
void A() { while (a() || b()) c(); }

// 2, 0, 1
void A() { while (1 + 2 == 4) c(); }

// 3, 0, 2
void A() { switch (a()) { case 1: b();} }

// 5, 0, 3
void A() { switch (a()) { case 1: b(); case 2: c();} }
        
// 1, 2, 2
void A() { a() ? 1 : 2; }

// 1, 2, 3
void A() { a() || b()? 1 : 2; }

// 1, 6, 4
void A() { a() ? b() ? c()? 1 : 2 : 3 : 4; }

 

References