Photo by Fernando Reyes on Unsplash

Code Coverage percentage comparison in your PRs

Fernando Prieto
ProAndroidDev
Published in
4 min readMay 21, 2023

--

Have you ever imagined being able to get some details about how much your latest pull request increased or decreased the total code coverage in the project that you are working on?

Now this is possible using Bitrise, Github and a couple of scripts. Let’s get stuck into it 👊🏽

Pre-requisites

  • A Jacoco plugin to generate code coverage from your unit tests. As an example, I will mention some gradle tasks that Android-Root-Coverage-Plugin offers. This SDK allows the use of a multi-module project and get either an overall or an individual module coverage report.
  • A Github repository linked with Bitrise.
  • Admin permissions on Bitrise, in order to be able to configure your Workflows.

Steps

  • Create a Comparator Workflow (unit_test_and_coverage_comparison). This workflow will be in charge of running the tests, create the code coverage of the PR branch and it will execute a code coverage comparison script, to then show these values comparison in the PR comments.
  • Configure the Bitrise PR Trigger (linking the previous Comparator Workflow), in order to run a build whenever a PR is raised.
This is the PR Trigger linked to the Comparator Workflow.
  • Create a Code Coverage Saver Workflow (unit_test_and_save_coverage). This workflow will be in charge of saving the latest code coverage in Bitrise Secrets, after running the unit tests and parse such value from the code coverage report.
  • Configure the Bitrise Push Trigger (linking the previous Code Coverage Saver Workflow), in order to run a build when the PR gets merged with main/master (or any other targeted) branch.
This is the Push Trigger linked to the Code Coverage Saver Workflow.
  • Generate a Bitrise Personal Access Token to be able to execute some Bitrise API calls to access the secrets from the scripts. Then set it up in your Bitrise Secrets, as a new variable (BITRISE_PERSONAL_ACCESS_TOKEN).
  • Generate a Github Personal access token. It will be needed by the Step that adds a message in the current PR. Then set it up in your Bitrise Secrets, as a new variable (GITHUB_BOT_CODE_COVERAGE).
  • Create another variable in your Bitrise Secrets (CODE_COVERAGE), to be able to save the code coverage at some point during the process.
These three secrets will be the ones needed for the scripts and Steps to work with
  • Raise a PR on Github, wait until the build passes all of the checks set up on Bitrise. And then, have a look at the Github message generated on your PR. This message will contain the code coverage comparison 🤩

Comparator Workflow

  • Clone your Github repository.
  • Configure your project, if necessary.
  • Run the unit tests in the project.
  • Execute Jacoco gradle task to generate reports (i.e. -Pcoverage rootCoverageReport).
  • Parse the code coverage percentage from the report file already generated.
  • Get the value, that contains the main/master branch code coverage percentage.
  • Set these two values in the environment variables provided by Bitrise (env man).
  • Use the Comment on GitHub Pull Request (Bitrise Step), in order to write a comment on the given PR, with the two code coverage percentages added to the environment variables. ⚠️ The very first time running this, the value obtained from the Bitrise Secrets, will be empty.
This is the text that will be showed in the PR message

Code Coverage Saver Workflow

  • Clone your Github repository.
  • Configure your project if necessary.
  • Run the unit tests in the project.
  • Execute Jacoco gradle task to generate reports (i.e. -Pcoverage rootCoverageReport).
  • Parse the code coverage percentage from the report file already generated.
  • Set that code coverage percentage in the Secrets. This new value will be available when a new PR is raised.
This excutes the last two tasks mentioned above.

--

--