This is my test:
// A test of TestSuite
func (ts *TestSuite) TestMyThing() {
cmd := RootCmd()
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetErr(b)
err := cmd.Execute()
fmt.Println(err)
// do some asserts etc
}
I can run my entire test suite without issue:
go test -timeout 30s -run ^TestSuite$ my/package
But when I try to run this specific test the testify args are being passed to Cobra for some reason.
This:
go test -timeout 30s -run ^TestSuite$ -testify.m ^TestMyThing$ my/package
Returns this error:
unknown shorthand flag: 't' in -testify.m
It appears its parsing -t
out of -testify.m
and sending it to my Cobra app.
Why is this happening? From examples I see online my command is correct right? I tried re-ordering the params in various ways, but I see the same behavior.
Edit
Ok I can merely set the args to null like this before calling Execute()
cmd.SetArgs([]string{""})
But I'm still confused about whats actually happening here. I guess I'm expecting testify to consume its own flags and not pass them along to the app like this. Is this expected behavior?
Edit
So I added this to one of my tests and I think I know what might be happening here:
for _, element := range os.Args {
println(element)
}
That gives me this output:
/tmp/go-build1970120879/b001/cmd.test
-test.testlogfile=/tmp/go-build1970120879/b001/testlog.txt
-test.paniconexit0
-test.v=test2json
-test.timeout=1m0s
-test.run=^TestSuite$
So I guess running go test with args is the same as running the actual application with args and Cobra is picking them up?
Reseting args with SetArgs()
is perfectly fine, but now I'm curious if there is a native go test or testify feature designed to control which args make it to the actual app.