Ignoring slow-running tests in Go

Featured image for sharing metadata for article

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!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #go #testing.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.