Closed a-dot closed 2 years ago
The couple ways I found to do this is to use flags.ParseArgs()
and supply it with a modified slice of arguments. I had to remove the -test.
arguments.
The cleaner way to do this I found is to create a new parser and use the IgnoreUnknown
option. Use the returned args
to modify os.Args
to do something like this os.Args = append([]string{os.Args[0]}, args...)
I've faced the same issue and could overcome it by using --
, like:
go test -v ./... -- -f 1s
In my case, the problem was also caused by the parallel tests execution. As it turns out, go runs tests in parallel when ./...
pattern passed (see https://engineering.mercari.com/en/blog/entry/20220408-how_to_use_t_parallel/.). And it means that each package (subfolder) should handle these additional arguments. Using --
helps to consider these following arguments as positional and thereby not be handled by go test
.
I tried different combinations for parsing command line arguments when using
go test
but could not get it to work.Option 1 -
flags.Parse
from a TestWhen running
go test -v ./... -args -f 1s
I getOption 2 -
flags.Parse
from TestMainWhen running
go test -v ./... -args -f 1s
I getOption 3 -
flags.Parse
from initWhen running
go test -v ./... -args -f 1s
I getHow are we supposed to use flags for command line arguments for tests? Thanks!