Go

How would you separate integration tests from unit tests using build tags? What alternatives exist?

Question 424MediumGo 1.22 to 1.25

Put a custom tag on the integration test files, then opt in when running tests:

//go:build integration

package store_test

import "testing"

func TestPostgresRoundTrip(t *testing.T) {
    dsn := mustEnv(t, "TEST_DSN")
    // ... real database
    _ = dsn
}

// run:
//   go test ./...                     // unit only
//   go test -tags=integration ./...   // include integration

Gotchas:

  • gopls and go vet don't see tagged files unless configured ("buildFlags": ["-tags=integration"]), so the files can rot without anyone noticing.
  • If every file in a package is tagged, an untagged build reports "build constraints exclude all Go files".
  • Test caching depends on the tag set, which is correct behavior.

Alternatives interviewers like to hear about:

  • testing.Short() with go test -short to skip slow tests.
  • Skipping when an environment variable is missing (t.Skip). The code is always compiled, so it cannot rot.
  • Separate TestMain setup using testcontainers.
  • Running by name with -run 'Integration'.

A senior answer: prefer env-based skipping so the code stays compiled and type-checked, and use tags only when the tests import heavy dependencies you want kept out of normal builds.

More on Modules, Packages & Tooling

All 36 Modules, Packages & Tooling questions