42

Hi 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 alle of the other test file saying: flag provided but not defined.

Have you seen this?

Regards

6 Answers 6

35

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.

4
  • 2
    Thanks, it still only works within the same package, but not when running all tests with: ./...
    – Chris G.
    Apr 18, 2015 at 18:41
  • 5
    @ChrisG. if you're testing multiple packages, any flags you provide must be valid in all of them. There's no way around that.
    – JimB
    Apr 20, 2015 at 18:42
  • Thanks! Do you know how to ignore a test with regexp -run?
    – Chris G.
    Apr 28, 2015 at 9:50
  • 2
    @ChrisG.: ignoring a test won't help. You're telling go to run go test on the ./... wildcard. That's not a single test run, it's testing every matching package in succession. Simply don't use ./... and test the packages you want to test.
    – JimB
    Apr 28, 2015 at 13:22
15

If you've migrated to golang 13, it changed the order of the test initializer, so it could lead to something like

flag provided but not defined: -test.timeout

as a possible workaround, you can use

var _ = func() bool {
    testing.Init()
    return true
}()

that would call test initialization before the application one. More info can be found on the original thread:

https://github.com/golang/go/issues/31859#issuecomment-489889428

0
3

do not call flag.Parse() in any init()

3
  • Ok thanks good to know, how would you then test it?
    – Chris G.
    Sep 8, 2021 at 9:30
  • 3
    You want to define the flags before calling them, which you can do in an init() as well. However, using init to do things makes testing difficult, and you're better off using a helper function off of main to do this work. Sep 8, 2021 at 12:13
  • 4
    Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Sep 8, 2021 at 12:14
3

I'm very late to the party; but is this broken (again) on Go 1.19.5?

I hit the same errors reported on this thread and the same solution reported above (https://github.com/golang/go/issues/31859#issuecomment-489889428) fixes it.

I see a call to flags.Parse() was added back in go_test.go in v1.18

https://go.googlesource.com/go/+/f7248f05946c1804b5519d0b3eb0db054dc9c5d6%5E%21/src/cmd/go/go_test.go

I am only just learning Go so it'd be nice to have some verification from people more skilled before I report this elsewhere.

1

If you get this, when running command via docker-compose then you do incorrect quoting. Eg.

services:
  app:
    ...
    image: kumina/openvpn-exporter:latest
    command: [  
      "--openvpn.status_paths", "/etc/openvpn_exporter/openvpn-status.log",
      "--openvpn.status_paths /etc/openvpn_exporter/openvpn-status.log",
    ]

First is correct, second is wrong, because whole line counted as one parameter. You need to split them by passing two separate strings, like in first line.

0

You can run flag.Parse() only one time, check if you are calling another code with flag.Parse()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.