V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  Sunshineplan  ›  全部回复第 1 页 / 共 1 页
回复总数  3
2022-03-22 11:52:13 +08:00
回复了 BeautifulSoap 创建的主题 Go 编程语言 go 泛型函数的单元测试实在是太"难"写了
```go
func runTestCases[T constraints.Ordered](t *testing.T, name string, a, b, want T) {
t.Run(name, func(t *testing.T) {
if got := Add(a, b); !reflect.DeepEqual(got, want) {
t.Errorf("xxxxxxxxxxxxxx")
}
})
}

func TestAdd(t *testing.T) {
runTestCases(t, "ok", 1, 1, 2)
runTestCases(t, "ok2", 10, 10, 20)
runTestCases(t, "ok", "A", "B", "AB")
runTestCases(t, "ok2", "Hello", "World", "HelloWorld")
}
```

这样行么?
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3155 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 16ms · UTC 14:20 · PVG 22:20 · LAX 07:20 · JFK 10:20
Developed with CodeLauncher
♥ Do have faith in what you're doing.