Go "build failed" During go test - Fix Test Compile Errors in CI
go test compiles each package together with its _test.go files into a test binary before running anything. When that compile fails, Go reports FAIL <pkg> [build failed] and no tests run - the error is in the test code or its dependencies, not in an assertion.
What this error means
go test ./... prints a compile error followed by FAIL <pkg> [build failed]. There are no --- FAIL assertion lines because the test binary never built, so nothing executed.
# github.com/yourorg/app/store [github.com/yourorg/app/store.test]
./store_test.go:12:14: undefined: NewStore
./store_test.go:20:2: declared and not used: got
FAIL github.com/yourorg/app/store [build failed]Common causes
A compile error in a _test.go file
The test file references a renamed/removed symbol, has an unused variable, or a type error. Test files compile under the same strict rules as production code.
A missing test-only dependency
A test imports a package (a testing helper, a mock library) that is not in go.mod/go.sum, so the test binary cannot build.
Test helpers out of sync with the code
A refactor changed a function signature but the test was not updated, so the test package fails to compile against the new API.
How to fix it
Build the test binary to see the error
Compile the tests without running them to isolate the compile failure.
go test -run xxx ./store/... # compiles tests, runs none
# or
go vet ./store/...Fix the test code
- Read the file:line - it points at the test compile error.
- Update the test to the current API, remove unused variables, or fix the type error.
- Add any missing test-only dependency with
go getandgo mod tidy.
Add missing test dependencies
go get github.com/stretchr/testify@latest
go mod tidyHow to prevent it
- Compile tests in CI (
go build ./...plusgo vet ./...) so they fail fast. - Update tests alongside the code when changing signatures.
- Keep test-only dependencies in go.mod and tidy after adding them.