单测里加参数处理代码:
import (
"github.com/spf13/pflag"
"flag"
"testing"
)
func TestAA(t *testing.T) {
myflag := ""
flagSet := pflag.NewFlagSet("TestAA", pflag.ContinueOnError)
flagSet.StringVar(&myflag, "myflag", "", "set myflag")
_ = flagSet.Parse(flag.CommandLine.Args())
println("myflag:", myflag)
//......
}
编译测试包:
go test -c aa_test.go aa.go -o testbin
查看测试用参数:
./testbin -test.run TestAA args --help
传参并执行测试:
./testbin -test.run TestAA args --myflag myparam
查看testing.Log的输出需要加 -test.v 参数:
./testbin -test.v -test.run TestAA args --myflag myparam