-2

In my go program, the main method does:

port := flag.Int("port", 8080, "Port number to start service on")
flag.Parse()

I have a dummy test that looks as:

func TestName(t *testing.T) {
    fmt.Println("Hello there")
}

when I run my tests (from the goland or the command line) I got the following error stuck:

/usr/local/go/bin/go tool test2json -t /private/var/folders/7v/p2t5phhn6cn9hqwjnn_p95_80000gn/T/___TestName_in_github_tools....test -test.v -test.paniconexit0 -test.run ^\QTestName\E$
flag provided but not defined: -test.v
Usage of /private/var/folders/7v/p2t5phhn6cn9hqwjnn_p95_80000gn/T/___TestName_in_github_tools.....test:
  -port int
        Port number to start service on (default 8080)

When I remove the lines of the flag in the main, the test executes normally Any idea on how to fix this, please?

Thanks in advance

Marwan Jaber
  • 611
  • 11
  • 27
  • 1. You _exacty_ do you run your tests? A simple (and correct) `go test` or some fancyness? 2. Why do you call testing.Init()? Which part of testing.Init's documentation made you think your code would benefit from it. 3. Your `func main()` is not called during execution of test so any modification of the global execution space (like what flag.Int does) is not executed. – Volker May 06 '21 at 09:51
  • Sorry, I guess you meant to ask 'how exactly do you run your tests?' Actually, I do 'go test' the call for testing.Init() was here by mistake I will edit the test – Marwan Jaber May 06 '21 at 09:58
  • 1
    Are you sure that you pass `flag.Parse()` to the `main()` function? It can happen if you pass it to `init()` function. Please see https://github.com/golang/go/issues/31859, especially https://github.com/golang/go/issues/31859#issuecomment-489889428 – s0xzwasd May 06 '21 at 10:02
  • If you are doing 'go test' why are you passing -test.v flag? Please add details on how you are running tests to the question. – Aruna Herath May 06 '21 at 10:22
  • test2json is not normaly called by running `go test`. Sou are not running a simple `go test`. Please include the _exact_ command line you use to run your tests in the question. – Volker May 06 '21 at 10:51
  • I execute "go test" and I get: "flag provided but not defined: -test.paniconexit0" – Marwan Jaber May 06 '21 at 10:52
  • 1
    Please, provide a full code snippet from your `main.go` and `filename_test.go`. – s0xzwasd May 06 '21 at 10:58

1 Answers1

6

When you run go test, go actually compiles your code into an executable, and executes it.
If you add options to go test -- for example : go test -v -- these options are actually passed to the test executable, prefixed with test -- so -v is turned into -test.v.

(this is a reason why several comments ask for the exact command line you use to run your tests : since the error is about -test.v, there probably is something that adds -v to some go test ... invocation)


It looks like flag.Parse() is trying to parse some arguments which are actually intended for your test executable, not for your code.

This is probably because it is called too early, before the test executable has had a chance to alter the os.Args slice to remove some specific flags.

Check what triggers a call to flag.Parse() : if it is executed from an init() block, this would count as "too early".


The behavior of go test options is documented in go help testflag :

Each of these flags is also recognized with an optional 'test.' prefix, as in -test.v. When invoking the generated test binary (the result of 'go test -c') directly, however, the prefix is mandatory.

The 'go test' command rewrites or removes recognized flags, as appropriate, both before and after the optional package list, before invoking the test binary.

For instance, the command

go test -v -myflag testdata -cpuprofile=prof.out -x

will compile the test binary and then run it as

pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Thank you for your answer, indeed this was the issue. I found a call to the flag.parse somewhere before the main function in my code, removed it and everything works as expected – Marwan Jaber May 16 '21 at 10:13
  • 1
    We can also add following code before flag.Parse() method in init() block var _ = func() bool { testing.Init() return true }() It will initialize the required test flags. – AmitD Jul 04 '22 at 08:58