Go "flag provided but not defined" in go test - Fix in CI
go test only accepts known flags, and their position relative to the package list matters. An unknown flag or one placed after the packages aborts the run.
What this error means
A run fails with flag provided but not defined: -foo and the test usage text. It usually means a typo in a flag, a custom flag not registered, or flags placed after the package arguments.
flag provided but not defined: -coverage
Usage of /tmp/go-build/app.test:
exit status 2Common causes
Misspelled or wrong flag
A flag name is wrong (e.g. -coverage instead of -cover), so go test rejects it.
Custom test flag not registered
A user-defined flag is passed but the test binary never declared it with the flag package.
Flags after the package list
Test flags placed after ./... are passed to the test binary differently and may be rejected.
How to fix it
Use the correct flag and order
- Spell the flag correctly and place go test flags before the package list.
go test -cover -coverprofile=cover.out ./...Register custom flags
- Declare any custom flag in the test package with the flag package before using it.
var slow = flag.Bool("slow", false, "run slow tests")How to prevent it
- Check flag names against
go help testflag. - Put test flags before the package arguments.
- Register custom flags before passing them.