我在 Go 上咬牙切齒,在深入研究表驅動測試后,我遇到了以下問題:我有一個返回多個值的函數// Halves an integer and and returns true if it was even or false if it was odd.func half(n int) (int, bool) { h := n / 2 e := n%2 == 0 return h, e}我知道half(1)返回值應該是0, false并且half(2)它應該匹配1, true,但我似乎無法弄清楚如何將它放在桌子上。怎么會有類似于以下內容的東西?var halfTests = []struct { in int out string}{ {1, <0, false>}, {3, <1, true>},}有沒有其他更慣用的方法來做到這一點?作為參考,這里有一個類似于 FizzBuzz 函數的測試,使用表:var fizzbuzzTests = []struct { in int out string}{ {1, "1"}, {3, "Fizz"}, {5, "Buzz"}, {75, "FizzBuzz"},}func TestFizzBuzz(t *testing.T) { for _, tt := range fizzbuzzTests { s := FizzBuzz(tt.in) if s != tt.out { t.Errorf("Fizzbuzz(%d) => %s, want %s", tt.in, s, tt.out) } }}
多返回值函數的表測試
幕布斯6054654
2021-11-29 16:35:00
