Skip to content

Instantly share code, notes, and snippets.

@TheMetalCode
Last active January 27, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheMetalCode/fd252452ebaaa0c75111f836eef1e860 to your computer and use it in GitHub Desktop.
Save TheMetalCode/fd252452ebaaa0c75111f836eef1e860 to your computer and use it in GitHub Desktop.
Extract The High-Level Coverage Stats from a Jacoco HTML Report

Let's say the following describes your situation:

  • You're using JaCoCo to measure code coverage for a Java/Kotlin project (eg an Android app), a perfectly good tool for the job that unfortunately does not generate JSON reports.
  • You're using a 3rd party SaaS provider like CircleCI, which can sometimes perform unpredictably when uploading large amounts of files to build artifact storage. Things like HTML coverage reports from JaCoCo, for instance.
  • As a result you'd like to zip the HTML report prior to saving it to artifacts but would still like the high-level coverage stats to be made transparent without having to download and unzip the HTML report.

If you're in this boat along with me, here is one way you might go about extracting this infromation from the html report.

The risk here is that this is theoretically brittle - it's tied to whatever the DOM happens to be for the HTML that JaCoCo generates. In reality, it hasn't changed in quite some time. YMMV.

#!/bin/bash
# Install pup: https://github.com/EricChiang/pup#install
# The assumption is that you've already executed tests with jacoco (https://www.jacoco.org/jacoco/trunk/doc/index.html)
# coverage implemented, and that it has generated an html report to coverage/html.
cat coverage/html/index.html | ./pup 'table#coveragetable.coverage tfoot tr td:nth-of-type(3) text{}' > coverage/totalLineCoverage.txt
cat coverage/html/index.html | ./pup 'table#coveragetable.coverage tfoot tr td:nth-of-type(5) text{}' > coverage/totalBranchCoverage.txt
echo "Total line coverage: $(cat coverage/totalLineCoverage.txt)"
echo "Total branch coverage: $(cat coverage/totalBranchCoverage.txt)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment