xhd2015 / xgo

All-in-one go testing library
MIT License
368 stars 19 forks source link

test-explorer: test binary complains flags not defined #263

Closed xhd2015 closed 2 months ago

xhd2015 commented 2 months ago

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

xhd2015 commented 2 months ago

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.