Ignoring slow-running tests in Go
If you're writing software in Go, you're likely to not be hitting any particularly slow tests, as the language and tooling is very efficient.
But sometimes there are tests that you don't want to execute when go test
is run, for instance if they're full integration/application tests, or are exercising external integrations like a database.
Fortunately, there's a built-in means to do this using go test
's -short
flag. This allows us to run:
# ./... for the whole module, or you can specify a given package
go test -short ./...
Then, in the tests, we can follow the documentation, and where we can self-select tests that will be likely to run slowly, we can skip them if running in short mode:
func TestTimeConsuming(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
// the test
}
This is super handy for isolating larger tests, and giving you the chance of making sure you have the fastest feedback for your code changes!