Essential Go Build Constraints  Edit on GitHub      File Issue

Separate integration tests

Build constraints are commonly used to separate normal unit tests from integration tests that require external resources, like a database or network access. To do this, add a custom build constraint to the top of the test file:

// +build integration

package main

import (

func TestThatRequiresNetworkAccess(t *testing.T) {
    t.Fatal("It failed!")

The test file will not compile into the build executable unless the following invocation of go test is used:

go test -tags "integration"


$ go test
?    [no test files]
$ go test -tags "integration"
--- FAIL: TestThatRequiresNetworkAccess (0.00s)
        main_test.go:10: It failed!
exit status 1
FAIL    0.003s
  ↑ ↓ to navigate     ↵ to select     Esc to close