[Translation]Golang中的表驱动测试

翻译自<Table driven tests in Go> by Alex Pliutau 2018/08/07git

原文连接github

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属性用来描述特定的测试用例。spa

有了表以后,咱们能够简单地进行迭代和断言:翻译

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()仅仅记录错误并继续测试。code

在Go中很流行的Testify断言包使单元测试明确,好比:get

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中进行表驱动测试吧!

相关文章
相关标签/搜索