Skip to content
Latchkey

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.

go
flag provided but not defined: -coverage
Usage of /tmp/go-build/app.test:
exit status 2

Common 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

  1. Spell the flag correctly and place go test flags before the package list.
.github/workflows/ci.yml
go test -cover -coverprofile=cover.out ./...

Register custom flags

  1. Declare any custom flag in the test package with the flag package before using it.
Go
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.

Frequently asked questions

What causes ""flag provided but not defined""?
A flag name is wrong (e.g. -coverage instead of -cover), so go test rejects it.
How do I fix "flag provided but not defined"?
Use the correct flag and order

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card