flag.Parse in tests

2320 views
Skip to first unread message

jonathan...@gmail.com

unread,
Jul 9, 2015, 3:42:32 PM7/9/15
to golan...@googlegroups.com
I can't seem to find this documented or examples shown.

When using a flag passed into a test when should flag.Parse be called?
I tried not calling it and the flag doesn't come through.
Calling it in init() seems to work, as does in the test case, as does in a "func TestMain()".

Ian Lance Taylor

unread,
Jul 9, 2015, 4:23:38 PM7/9/15
to jonathan...@gmail.com, golang-nuts
The testing package calls flag.Parse itself. I don't know why you
would not see the flag. How are you defining the flag?

Ian

jonathan...@gmail.com

unread,
Jul 9, 2015, 7:00:33 PM7/9/15
to golan...@googlegroups.com, jonathan...@gmail.com
within func init() { }
I am doing flag.StringVar(&someVar, "someName", "", "desc")

and within the pkg dir doing go test -v -someName boo

Rob Pike

unread,
Jul 9, 2015, 8:55:14 PM7/9/15
to jonathan...@gmail.com, golan...@googlegroups.com
The flag is not known to go test; you should see a usage message that makes that clearer. You need to build the binary first, and then run the binary with the flag:

$ go test -c 
$ foo.test -someName boo

-rob


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jonathan...@gmail.com

unread,
Jul 10, 2015, 2:19:51 AM7/10/15
to golan...@googlegroups.com, jonathan...@gmail.com
Thanks for the help Rob. Narrowed it down with a little test:

package a

import (
    "flag"
    "fmt"
    "os"
    "testing"
)

var theflag string

func init() {
    flag.StringVar(&theflag, "theflag", "", "desc")
}

func TestMain(m *testing.M) {
    fmt.Printf("'%v'\n", theflag)

    os.Exit(m.Run())
}

func TestFoo(t *testing.T) {
    fmt.Printf("'%v'\n", theflag)
}

Within TestMain it prints blank but prints correctly in the TestFoo, is this expected?
If TestMain's utility is for setting up test data etc, then having the flags available is very useful.
If I call Parse within TestMain it does work correctly which seems analogous to the normal func main, so I am guessing this is the intended functionality. Good thing for future docs :)
Thanks! 

Rob Pike

unread,
Jul 10, 2015, 2:43:28 AM7/10/15
to jonathan...@gmail.com, golan...@googlegroups.com
I missed that you were using TestMain. As the documentation for package testing says,

A simple implementation of TestMain is:

    func TestMain(m *testing.M) {
    flag.Parse()
    os.Exit(m.Run())
    }

You need to call flag.Parse in TestMain.

In other words, it's already in the docs.

-rob

jonathan...@gmail.com

unread,
Jul 10, 2015, 3:02:10 AM7/10/15
to golan...@googlegroups.com, jonathan...@gmail.com
Is this the upcoming 1.5 docs?

I see:
The minimal implementation of TestMain is: 
func TestMain(m *testing.M) { os.Exit(m.Run()) } 

By the way the 1.5 change log looks fantastic ! 

Rob Pike

unread,
Jul 10, 2015, 3:06:05 AM7/10/15
to jonathan...@gmail.com, golan...@googlegroups.com
Yes, looks like it's in 1.5

-rob

Reply all
Reply to author
Forward
0 new messages