How are the Clover coverage percentages calculated?

Q: How are the Clover coverage percentages calculated?The "total" coverage percentage of a class (or file, package, project) is provided as a quick guide to how well the class is covered — and to allow ranking of classes.

The Total Percentage Coverage (TPC) is calculated using the formula:

TPC = (CT + CF + SC + MC)/(2*C + S + M){excerpt}

where

CT - conditionals that evaluated to "true" at least once
CF - conditionals that evaluated to "false" at least once
SC - statements covered
MC - methods entered

C - total number of conditionals
S - total number of statements
M - total number of methods

 

Coverage metrics in XML report file

The XML report file produced by Clover contains the <metrics> tag with a number of attributes. For instance:

<metrics
    coveredelements="221" complexity="123" loc="707" methods="100" classes="37" 
    statements="209" packages="1" 
    coveredconditionals="12" coveredmethods="42" 
    elements="337" ncloc="379" files="11" 
    conditionals="28" coveredstatements="167"/>

 

A mapping between the equation and these attributes is as follows:

  • CT + CF = coveredconditionals
  • SC = coveredstatements
  • MC = coveredmethods
  • 2 * C = conditionals
  • S = statements
  • M = methods

In other words, the 'conditionals' attribute is already a doubled number of branches, while the 'coveredconditionals' attribute is a sum of branches evaluated to true and branches evaluated to false.

So in order to calculate the Total Percentage Coverage metric using data from an XML report you have to use the following equation:

TPC = (coveredconditionals + coveredstatements + coveredmethods) / (conditionals + statements + methods)

 

Note:

The <metrics> tag in XML report contains one extra attribute pair, which is calculated as follows:

  • coveredelements = coveredconditionals + coveredstatements
  • elements = conditionals + statements