How to create GitHub Actions test summaries for Go

Go, Golang, GitHub Actions

One of my pet peeves is searching CI output for test failure details. While I realize that in many cases this is a necessity, in most cases I think this can be avoided using test output summaries. Luckily, there are two open source tools we can take advantage of to easily implement test summaries in GitHub Actions and Go tests.

First we need to generate the test summary xml file. The tool I commonly use across my test running needs is gotestyourself/gotestsum. This is a great tool for grouped console outputs and generating nice junit xml outputs.

Install with:

go install gotest.tools/gotestsum@latest

gotestsum is a drop-in replacement for go test, so we can someone easily replace this in our GitHub Action workflow:

	- name: Test
	run: go run gotest.tools/gotestsum@latest --junitfile unit-tests.xml --format pkgname

This simple workflow step will run the tests in the current folder, grouping by package name, and save the summary to a unit-tests.xml file. In cases where there are different styles of tests, (eg. unit, integration, e2e) running this multiple type and creating sepearate junitfiles is helpful.

Now we need to run our final step of persisting the test files as action summaries, to do this we will take advantage of the test-summary/action project. Added to our workflow file as:

	- name: Test Summary
	uses: test-summary/action@v2
	with:
		paths: "unit-tests.xml"
	if: always()

This step will persist the test summaries and make them accessible to GitHub Actions for display. Providing an output like:

A screenshot of GitHub Actions build summary with test results.

Full Workflow Sample:

name: build

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Set up Go 1.19
        uses: actions/setup-go@v1
        with:
          go-version: 1.19
        id: go

      - name: Check out code
        uses: actions/checkout@v1

      - name: Test
        run: go run gotest.tools/gotestsum@latest --junitfile unit-tests.xml --format pkgname

      - name: Test Summary
        uses: test-summary/action@v2
        with:
          paths: "unit-tests.xml"
        if: always()