unit-testinggogo-testinggo-flag

go test flag: flag provided but not defined


I am using a flag when testing in go:
file_test.go var ip = flag.String("ip", "noip", "test")

I am only using this in one test file. And it works fine when only testing that one test file, but when I run: go test ./... -ip 127.0.0.1 all of the other test files saying: flag provided but not defined.

Have you seen this?

Regards


Solution

  • flag.Parse() is being called before your flag is defined.

    You have to make sure that all flag definitions happen before calling flag.Parse(), usually by defining all flags inside init() functions.