18

[Translation]Golang中的表驱动测试

 3 years ago
source link: https://studygolang.com/articles/29983
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

有疑问加站长微信联系

iiUfA3j.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK