Level up go test with gotestsum
Tags: go, testing, circleci, cicd
Published: 2018-11-21 02:19:25

---
title: Level up go test with gotestsum
published: true
tags: go, testing, circleci, cicd
canonical_url: https://circleci.com/blog/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][blog-go111], running tests is as simple as:

```Bash
# 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.

<!-- more -->

## gotestsum

`gotestsum` is a [CLI][1] 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][gotestsum], it "runs tests, and prints friendly test output and a summary of the test run."

Here's how it looks:

![](https://thepracticaldev.s3.amazonaws.com/i/nz68e561yg3zxg5cbhuk.gif)

That's pretty cool for local development, but what about using it for continuous integration ([CI](https://circleci.com/continuous-integration/))?


## 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][docs-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][dhub-golang] (`circleci/golang`) then congratulations, `gotestsum` is already pre-installed for you! If not, I'd suggest following the [installing locally](#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](https://github.com/dnephin), an engineer at CircleCI.


  
[blog-go111]: https://circleci.com/blog/go-v1.11-modules-and-circleci/
[gotestsum]: https://github.com/gotestyourself/gotestsum
[dhub-golang]: https://hub.docker.com/r/circleci/golang/
[docs-test-metadata]: https://circleci.com/docs/2.0/collect-test-data/
[1]: https://en.wikipedia.org/wiki/Command-line_interface