43

I am using a flag when testing in go:
file_test.govar 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

Guy Avraham's user avatar
Guy Avraham
3,7583 gold badges42 silver badges55 bronze badges
askedApr 17, 2015 at 13:01
Chris G.'s user avatar

6 Answers6

39

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

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

answeredApr 17, 2015 at 13:11
Mr_Pink's user avatar
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it still only works within the same package, but not when running all tests with: ./...
@ChrisG. if you're testing multiple packages, any flags you provide must be valid in all of them. There's no way around that.
Thanks! Do you know how to ignore a test with regexp -run?
@ChrisG.: ignoring a test won't help. You're telling go to rungo 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.
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

answeredOct 1, 2019 at 21:12
lanwen's user avatar

Comments

4

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 toflags.Parse() was added back ingo_test.go inv1.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.

answeredFeb 10, 2023 at 11:20
manstis's user avatar

Comments

4

Do not callflag.Parse() in anyinit() method

Guy Avraham's user avatar
Guy Avraham
3,7583 gold badges42 silver badges55 bronze badges
answeredSep 8, 2021 at 9:24
hermosayang's user avatar

3 Comments

Ok thanks good to know, how would you then test it?
You want to define the flags before calling them, which you can do in aninit() 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.
Please add further details to expand on your answer, such as working code or documentation citations.
1

If you get this, when running command viadocker-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.

answeredOct 26, 2022 at 22:52
Eugen Konkov's user avatar

Comments

0

You can runflag.Parse() only one time, check if you are calling another code withflag.Parse()

Guy Avraham's user avatar
Guy Avraham
3,7583 gold badges42 silver badges55 bronze badges
answeredAug 11, 2023 at 22:16
Hoiama Rodrigues's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.