DEV Community

Ricardo N Feliciano for CircleCI

Posted on • Originally published at circleci.com

Level up go test with gotestsum

Hey Gophers!

One of the many great things about Go (Golang) is how simple it is to run tests. With the changes we saw in Go v1.11, running tests is as simple as:

# To test only your current module
go test ./...

# To test your module as well as its direct and indirect dependencies
go test all

In this post, I'm going to show you how we can make the output of go test more useful for local development as well as for CircleCI.

gotestsum

gotestsum is a CLI tool written in Go that runs go test for you. It enhances the output, making it much more palatable. As described on its GitHub repository, it "runs tests, and prints friendly test output and a summary of the test run."

Here's how it looks:

That's pretty cool for local development, but what about using it for continuous integration (CI)?

CircleCI / JUnit support

Most CI providers support saving tests results in the JUnit format. What the CI provider does with it can vary, but this usually means accessing historical test data, cool charts and graphs, etc. CircleCI supports collecting test metadata as long as it's in JUnit format. Since go test doesn't output in the JUnit format natively, we'll use gotestsum to do that for us.

gotestsum --junitfile unit-tests.xml

The command above will output your test results in the JUnit format. You can then tell CircleCI to use that file as test metadata via your CircleCI config.xml file.

    - store_test_results:
        path: /tmp/test-results

Installing gotestsum

Requirements

  • Go v1.10 or later

Installing locally

As with most Go applications, running gotestsum can be as simple as placing the binary on your $PATH:

curl -sSL "https://github.com/gotestyourself/gotestsum/releases/download/v0.3.1/gotestsum_0.3.1_linux_amd64.tar.gz" | sudo tar -xz -C /usr/local/bin gotestsum 

The above command installs v0.3.1 of gotestsum, the most recent version as of this writing. If a new version is available, update the version number in the URL. You can also replace linux in the URL with darwin for macOS users. The binary packages available are for 64-bit x86 systems. See the next section to compile your own.

Compiling a binary

If you have a 32-bit system, ARM, or some other need, here's how to compile and install a custom binary:

go get -u gotest.tools/gotestsum

Installing on CircleCI

If you are using a Golang CircleCI Docker Convenience Image (circleci/golang) then congratulations, gotestsum is already pre-installed for you! If not, I'd suggest following the installing locally instructions and applying that to your CircleCI config.yml file or, for a custom image, within a Dockerfile.

gotestsum is a project maintained by Daniel Nephin, an engineer at CircleCI.

Top comments (0)