xhd2015/xgo

test-explorer: test binary complains flags not defined

Closed this issue · 1 comments

The following test:

package args

func TestArgs(t *testing.T){
    t.Logf("args: %v", os.Args)
}

When run with:

go test -v ./ -args a b c

Will print a list of test flags like -test.v, along with a b c.

However, when run with

go test -v ./ -args -a b c

The program complains about flag a provided but not defined

The reason is that, when go emits test binary, the binary itself will invoke flag.Parse(), which then will try to parse all flags on the command line, even when the program itself will parse these flags correctly.

To suppress this behavior, we just need to add a -- after -args, which will then prevent flag.Parse() from consuming the reminder args.