内容简介:翻译自<Table driven tests in Go> by Alex Pliutau 2018/08/07在
翻译自<Table driven tests in Go> by Alex Pliutau 2018/08/07
在 practice-go 中我们经常使用表驱动测试来测试所有可能的函数使用场景。例如 FindAnagrams() 函数对于给定输入返回字典中找到的字谜列表。
为了能够正确测试 FindAnagrams() 函数,我们需要测试各种情况,例如空输入,有效输入,无效输入等。我们可以修改不同断言来实现测试,但是使用表测试要容易得多。
假设有以下函数:
FindAnagrams(string word) []string
这是“表”看起来的样子:
var tests = []struct {
name string
word string
want []string
}{
{"empty input string", "", []string{}},
{"two anagrams", "Protectionism", []string{"Cite no imports", "Nice to imports"}},
{"input with space", "Real fun", []string{"funeral"}},
}
通常,表是匿名结构体数组切片,可以定义结构体或使用已经存在的结构进行结构体数组声明。 name 属性用来描述特定的测试用例。
有了表之后,我们可以简单地进行迭代和断言:
func TestFindAnagrams(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FindAnagrams(tt.word)
if got != tt.want {
t.Errorf("FindAnagrams(%s) got %v, want %v", tt.word, got, tt.want)
}
})
}
}
可以使用其他函数代替 t.Errorf() 函数, t.Errorf() 仅仅记录错误并继续测试。
在 Go 中很流行的 Testify 断言包使单元测试明确,比如:
assert.Equal(t, got, tt.want, "they should be equal")
t.Run() 将启动一个子测试,并且如果您以详细模式运行测试( go test -v ),您将看到每个子测试结果:
=== RUN TestFindAnagrams === RUN TestFindAnagrams/empty_input_string === RUN TestFindAnagrams/two_anagrams === RUN TestFindAnagrams/input_with_space`
由于Go 1.7测试包允许使用 (*testing.T).Parallel() 来并行子测试。请确保并行化测试是有意义的!
t.Run(tt.name, func(subtest *testing.T) {
subtest.Parallel()
got := FindAnagrams(tt.word)
// assertion
})
就是这样,享受在Go中进行表驱动测试吧!
有疑问加站长微信联系
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 架构视角 - DDD、TDD、MDD领域驱动、测试驱动还是模型驱动?
- 使用“数据驱动测试”之前
- 《测试驱动开发》的读书笔记
- 搭建数据驱动测试框架构
- .netcore持续集成测试篇之Xunit数据驱动测试
- 在敏捷中应用测试驱动开发
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。