在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中进行表驱动测试吧!
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理