Getting some insights from Jacoco html report

Reading Time: < 1 minutes

Jacoco is a tool used to generate detailed reports on code coverage. It is an essential part of a software developer’s toolkit, since it helps determine what portions of a project’s code were actually tested. Jacoco produces HTML reports that display code coverage information in an easy to read format.

Context

After running the Jacoco report generator we can open it and see the coverage results. There are some results the I wanted to know, for example, how many packages contain uncovered classes and how many contain coverage under a given threshold, in this case, I used 60%. So I build a script to count them and print in the browser’s console. So, step into Jacoco’s report tab and paste this script in your browser console to execute it and see the output

Script

      
const table = document.getElementById("coveragetable");
const row = table.rows;
let uncoveredPackages = 0;
let columnFound = false; // the instructions coverage
let thresholdCoverage = 60;
let packagesUnderThreshold = 0;
let totalPackages = 0;
let i = 0;
while (!columnFound && i < row[0].cells.length) {
  // Getting the text of columnName
  const str = row[0].cells[i].innerHTML;
  if (str.search("Cov.") != -1) {
    if (!columnFound) {
      // this is the first column with coverage for instructions the next "Cov." column is the one for branches
      columnFound = true;
      // iterate every row but the head and foot
      for (let j = 1; j < row.length - 1; j++) {
        const content = row[j].cells[i].innerHTML;
        const cov = parseFloat(content);
        totalPackages++;
        if (cov === 0) {
          uncoveredPackages++;
        }
        if (cov < thresholdCoverage) {
          packagesUnderThreshold++;
        }
      }
    }
  }
  i++;
}
console.log(`total packages: ${totalPackages}`);
console.log(`uncovered packages: ${uncoveredPackages}`);
console.log(`packages under  ${thresholdCoverage}%: ${packagesUnderThreshold}`);

      
    

Output

total packages: 226

uncovered packages: 60

packages under 60%: 157

Photo by Ryutaro Uozumi on Unsplash

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: