2

I am working on a customized application to parse through the clover.xml report. Just wondering if anybody knows which is the correct formula to get the Classes and Traits total coverage percentage.

Here's the formulas that I found for Lines and Functions&Methods coverage:

// TPC = (coveredconditionals + coveredstatements + coveredmethods) / (conditionals + statements + methods)
phpMetric.project = (((phpMetric.coveredconditionals + phpMetric.coveredstatements + phpMetric.coveredmethods) / (phpMetric.conditionals + phpMetric.statements + phpMetric.methods))) * 100;
// Lines coverage formula - LTPC = (coveredstatements / statements) * 100
phpMetric.lines =  ((phpMetric.coveredstatements / phpMetric.statements)* 100);
// Functions and Methods coverage formula - FMTPC = (coveredmethods / methods) * 100
phpMetric.functions =  ((phpMetric.coveredmethods / phpMetric.methods)* 100);

And this is the metrics from clover.xml

<metrics files="10070" loc="1354443" ncloc="1110810" classes="8575" methods="46082" coveredmethods="31707" conditionals="0" coveredconditionals="0" statements="561696" coveredstatements="371009" elements="607778" coveredelements="402716"/>

Update: The information that I wanna extract from the report is the column Classes and Traits, the total coverage %. Total Coverage Report

Thanks!

Everton Wcks
  • 99
  • 1
  • 6
  • What exactly are you trying to extract from the xml? – Jack Fleeting Nov 12 '20 at 23:48
  • @JackFleeting - I'm trying to extract the overall coverage data for each coverage category, such as: Lines Coverage, Functions and Methods Coverage and Classes and Traits. For lines and Functions/Methods, I already got the data from the xml, but for the Classes coverage I didn't. – Everton Wcks Nov 13 '20 at 14:02
  • I'm afraid it's still unclear, as I have no idea what these things are. Focus on the xml: you have there one element, named `` with several attributes (`files`, `loc`, `ncloc`, etc), each of which has an attribute value (`10070`, `1354443` and `1110810`, respectively, etc.). Which of these **specifically** do you need to extract in order to create the report? – Jack Fleeting Nov 13 '20 at 14:25
  • Yes, this is I'm looking for. For example: In the element `metrics` we have the information for methods: `methods="46117" coveredmethods="31733"` - How I can extract the total coverage percentage: `TCP = (coveredmethods / methods) * 100` . Unfortunately, I didn't identify the coveredClasses into the element `metrics`. Not sure how to /where is the coveredClasses elements or something else. The element has the value `classes="8579"` but not `coveredClasses`. Thanks @JackFleeting – Everton Wcks Nov 13 '20 at 15:25

1 Answers1

2

I don't know anything about clover, but - if I understand you correctly - you can use php (which is tagged in your question) do something like the following. Obviously, you can then modify it as necessary:

$cloverstr = <<<SOMEXML
<root>
   <metrics files="10070" loc="1354443" ncloc="1110810" classes="8575" methods="46082" coveredmethods="31707" conditionals="0" coveredconditionals="0" statements="561696" coveredstatements="371009" elements="607778" coveredelements="402716" />
</root>    
SOMEXML;
$xml  = simplexml_load_string ($cloverstr);

$coveredmethods = $xml->xpath("//metrics/@coveredmethods");
$methods = $xml->xpath("//metrics/@methods");

$coveredstatements = $xml->xpath("//metrics/@coveredstatements");
$statements = $xml->xpath("//metrics/@statements");

$coveredelements = $xml->xpath("//metrics/@coveredelements");
$elements = $xml->xpath("//metrics/@elements");

$TCP = ($coveredmethods[0] / $methods[0]) * 100;
$LTPC = ($coveredstatements[0] / $statements[0]) * 100;
$XYZ = ($coveredelements[0] / $elements[0]) * 100;

echo ("TCP: " . $TCP . ' </br> ');
echo ("LTPC: " . $LTPC . ' </br> ');
echo ("XYZ: " . $XYZ);

Output:

TCP: 68.805607395512 
 LTPC: 66.0515652595 
 XYZ: 66.260377966955 
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45