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

Luis Carrazana
Apr 28 · 2 min read

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

Recording the digital DNA of financial services.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store