Travis/AppVeyor CI Script for Go

Continous Integration tests are an important part of maintaining package stability. Since Travis has recently made it possible to run tests on OSX and with AppVeyor having Windows support, we now can create free CI tests for all the major desktop platforms.

For my various packages, I find that I commonly copy+paste my CI tests, sometimes missing changes made. So when I set up AppVeyor for an old project, I thought I might be a good idea to share my “base” scripts for go testing. It will of course run your tests, but it will also check formatting/vet/lint on the code. This is to help with pull-requests, so these issues are worked out from the beginning.

These script are of course only meant as a base. You might want to add some custom things, like database setup, specific tests, build examples, check with various build tags and much more. I have considered adding cross-compilation to test that other platforms can compile, which is useful if you use syscall package or similar things. I might cover that in a separate post. Meanwhile have a look at restic, which handles this nicely.

"Exam" by Alberto G.
Test all the time. “Exam” by Alberto G. (CC BY 2.0)

Travis CI

This is a good base for your tests on Linux and OSX. It will check 3 versions of Go, as well as “tip”, which can give you an indication of future issues with your package, which are really rare, but with this you have a chance to catch them early.

The first check is a go vet check of your code. It will pass only if there are no issues.

The second check is if the code conforms to go fmt which is included in goimports, that helps order your imports. If you don’t want your imports to be checked, exchange goimports with gofmt and remove the `go get`.

The last check is a golint check of your code. It is of course entirely up to you, if you want to enforce complete golint compliance on your code. In my experience, if you start with it, it isn’t a big deal to keep doing it, and usually fixing it is quick.

Without further delay, here is my personal base `.travis.yml` base file:


language: go

sudo: false

os:
  - linux
  - osx

go:
  - 1.3
  - 1.4
  - 1.5
  - tip

install:
 - go get -t ./...
 - go get -u github.com/golang/lint/golint
 - go get -u golang.org/x/tools/cmd/goimports

script:
 - go vet ./...
 - diff <(goimports -d .) <(printf "")
 - diff <(golint ./...) <(printf "")
 - go test -v -cpu=2 ./...
 - go test -v -cpu=1,2,4 -short -race ./...

Update (3 Dec 2015): Added -t to `go get` to also retrieve test dependencies.

Other than the syntax checks, we also run a series of `go test`. We run the full test with 2 CPU’s enabled. This is the base test.

After that we run `go test` with the race detector enabled. The `-short` is an indication that long-running tests should be skipped/shortened.
If you have tests that takes a long time to complete, you can use the testing.Short() function to run a subset of the tests, or skip it entirely.

We run the race test with various CPU settings. This is because that race conditions may not be triggered with all CPU settings, and running it multiple times will make it much more likely to trigger.

AppVeyor CI

This script is meant as a supplement to the Travis CI above. That means we don’t run the same tests for formatting as we did in the Travis version. We do however want to run a complete test on Windows, so this contains the same `go test` as the Travis script.

You need to change NAME\REPO with your username and repository, for instance klauspost\compress.

Here is my `appveyor.yaml` file:


version: "{build}"

os: Windows Server 2012 R2

clone_folder: c:\gopath\src\github.com\NAME\REPO

environment:
  GOPATH: c:\gopath

install:
  - echo %PATH%
  - echo %GOPATH%
  - go version
  - go env
  - go get -d ./...

build_script:
 - go test -v -cpu=2 ./...
 - go test -cpu=1,2,4 -short -race ./...

I hope this will give you a good basis for adding CI tests to your project.

If you have good tests that are part of your “standard” tests, that you would like to share, you are very welcome to add them below. I will attempt to keep this list updated.

Flattr this!