How to set up a Test Coverage threshold in Go and Github

Luis Carrazana
Synechron
Published in
2 min readApr 28, 2021

This article contains steps to implement a Test Coverage Threshold for a project written in Golang using Github Actions as the CI/CD pipeline. For full context around why this is needed, please refer to the original article here: How to increase test coverage without stopping development.

Prerequisites

The main purpose of this article is to setup the test coverage threshold, therefore a few assumptions are made:

  • Familiarity with Go language and unit testing
  • Basic understanding of Github Actions

Steps to setup GitHub workflow with test coverage threshold

  1. If you haven’t done so, create the ci.yml manifest file for GitHub workflow
  2. Define a new step in CI workflow for setting the threshold
  3. Define a workflow variable to store the desire threshold value
  4. Add script to calculate current test coverage, compare to threshold and fail or pass accordingly

A very basic project containing above configuration and script can be found here.

Big kudos to Igor Domrev, whose previous work served as inspiration for this implementation.

Expected behavior

After setting up the test coverage threshold, the following behavior is expected:

  • A new GitHub CI workflow shall be started for every new pull request
  • If test coverage for the pull request is equal or greater than defined threshold, the build workflow shall continue
  • If test coverage for the pull request is lower than defined threshold, the build workflow shall fail

--

--

Luis Carrazana
Synechron

Software Engineer, Agilist, DevOps Champion, Cloud Engineer, Software Craftmanship - Specializing in financial services technology