How to skip long running test in Go

Here’s a quick tip on how to skip long running tests in Go.

In your test function that you want to skip, add a t.Short() boolean check.

For example:

1func TestLongRunning(t *testing.T) {
2    if t.Short() {
3        t.Skip("skip long running test")
4    }
5    // tests that will take a while to run
6    ...

Then when you run go test, simply provide a -short flag to skip the test.

For example:

1$~ go test -v -short

You can confirm this behaviour by checking test output. You should see skipping long running test in short mode message in the output.

For example:

1--- SKIP: TestLongRunningTest (0.00s)
2    main_test.go:27: skipping long running test in short mode
4ok       0.385s

That’s it. Hope you’ll find this tip useful.