对于测试函数,我可以选择由选项-run运行的函数。
go test -run regex如果我们有几十个测试用例,为了不为每个测试用例编写函数,将其放入数组中是很常见的:
cases := []struct {
arg, expected string
} {
{"%a", "[%a]"},
{"%-a", "[%-a]"},
// and many others
}
for _, c := range cases {
res := myfn(c.arg)
if res != c.expected {
t.Errorf("myfn(%q) should return %q, but it returns %q", c.arg, c.expected, res)
}
}这项工作很好,但问题在于维护。当我在调试时添加新的测试用例时,我只想启动一个新的测试用例,但我不能这样说:
go test -run TestMyFn.onlyThirdCase有没有什么好的方法,如何将多个测试用例放在数组中,并能够选择要运行的测试用例?
发布于 2016-08-12 14:59:26
使用Go 1.6 (及更低版本)
Go 1.6及更低版本中的testing包不直接支持此功能。你必须自己去实现它。
但这并不难。您可以使用flag包轻松访问命令行参数。
让我们来看一个例子。我们定义了一个"idx"命令行参数,如果该参数存在,则只执行该索引处的用例,否则将执行所有测试用例。
定义标志:
var idx = flag.Int("idx", -1, "specify case index to run only")解析命令行标志(实际上,这不是必需的,因为go test已经调用了它,但只是为了确保/ complete):
func init() {
flag.Parse()
}使用此参数:
for i, c := range cases {
if *idx != -1 && *idx != i {
println("Skipping idx", i)
continue
}
if res := myfn(c.arg); res != c.expected {
t.Errorf("myfn(%q) should return %q, but it returns %q", c.arg, c.expected, res)
}
}使用3个测试用例进行测试:
cases := []struct {
arg, expected string
}{
{"%a", "[%a]"},
{"%-a", "[%-a]"},
{"%+a", "[%+a]"},
}不带idx参数:
go test输出:
PASS
ok play 0.172s指定索引:
go test -idx=1输出:
Skipping idx 0
Skipping idx 2
PASS
ok play 0.203s当然,您可以实现更复杂的过滤逻辑,例如,您可以使用minidx和maxidx标志来运行某个范围内的案例:
var (
minidx = flag.Int("minidx", 0, "min case idx to run")
maxidx = flag.Int("maxidx", -1, "max case idx to run")
)和过滤:
if i < *minidx || *maxidx != -1 && i > *maxidx {
println("Skipping idx", i)
continue
}使用它:
go test -maxidx=1输出:
Skipping idx 2
PASS
ok play 0.188s从Go 1.7开始
Go 1.7 (将于2016年8月18日发布)增加了subtests and sub-benchmarks的定义
测试包现在支持有子测试的测试和有子基准的基准测试的定义。这种支持使得编写表驱动的基准测试和创建分层测试变得很容易。它还提供了一种共享公共设置和拆卸代码的方法。有关详细信息,请参阅package documentation。
有了它,你就可以做如下事情:
func TestFoo(t *testing.T) {
// <setup code>
t.Run("A=1", func(t *testing.T) { ... })
t.Run("A=2", func(t *testing.T) { ... })
t.Run("B=1", func(t *testing.T) { ... })
// <tear-down code>
}其中的子测试名为"A=1","A=2","B=1"。
-run和-bench命令行标志的参数是正则表达式的斜杠分隔列表,这些正则表达式依次匹配每个name元素。例如:
去测试-run Foo #运行与"Foo“匹配的顶层测试。go test -run Foo/A= #运行匹配"A=“的Foo的子测试。go test -run /A=1 #运行匹配"A=1“的顶级测试的所有子测试。
这对你的情况有什么帮助?子测试的名称是string值,可以动态生成,例如:
for i, c := range cases {
name := fmt.Sprintf("C=%d", i)
t.Run(name, func(t *testing.T) {
if res := myfn(c.arg); res != c.expected {
t.Errorf("myfn(%q) should return %q, but it returns %q",
c.arg, c.expected, res)
}
})
}要在索引2中运行案例,您可以像这样启动它
go test -run /C=2或
go test -run TestName/C=2发布于 2016-08-13 01:15:24
我编写了一个简单的代码,可以很好地使用这两个命令行选项,尽管命令行选项略有不同。1.7的版本是:
// +build go1.7
package plist
import "testing"
func runTest(name string, fn func(t *testing.T), t *testing.T) {
t.Run(name, fn)
}和1.6及更早版本:
// +build !go1.7
package plist
import (
"flag"
"testing"
"runtime"
"strings"
"fmt"
)
func init() {
flag.Parse()
}
var pattern = flag.String("pattern", "", "specify which test(s) should be executed")
var verbose = flag.Bool("verbose", false, "write whether test was done")
// This is a hack, that a bit simulate t.Run available from go1.7
func runTest(name string, fn func(t *testing.T), t *testing.T) {
// obtain name of caller
var pc[10]uintptr
runtime.Callers(2, pc[:])
var fnName = ""
f := runtime.FuncForPC(pc[0])
if f != nil {
fnName = f.Name()
}
names := strings.Split(fnName, ".")
fnName = names[len(names)-1] + "/" + name
if strings.Contains(fnName, *pattern) {
if *verbose {
fmt.Printf("%s is executed\n", fnName)
}
fn(t)
} else {
if *verbose {
fmt.Printf("%s is skipped\n", fnName)
}
}
}https://stackoverflow.com/questions/38911566
复制相似问题