Separate integration tests

suggest change

A common use for build constraints is separating unit tests from integration tests that require external resources such as a database or network service.

To do this, add a custom build constraint to the top of the test file:

//go:build integration

package main

import "testing"

func TestThatRequiresNetworkAccess(t *testing.T) {
	t.Fatal("integration test failed")
}

The file is excluded unless the integration tag is enabled. Run ordinary tests with go test ./... and include integration tests with go test -tags=integration ./....

Results:

$ go test
?       bitbucket.org/yourname/yourproject    [no test files]
$ go test -tags "integration"
--- FAIL: TestThatRequiresNetworkAccess (0.00s)
        main_test.go:10: integration test failed
FAIL
exit status 1
FAIL    bitbucket.org/yourname/yourproject    0.003s

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents