Table Driven Tests
This is an example, a trivial example, to the test driven testing in golang.
func TestSumExampleInGo(t *testing.T) {
var tests = []struct {
first int
second int
result int
}{
{2, 2, 4},
{3, 2, 5},
}
for _, test := range tests {
realSum := test.first + test.second
if realSum != test.result {
t.Error("wrong sum calculation")
}
}
}
Same thing using subtests
This is another way to test, introduced in Go 1.7.
func TestSumExampleInGo(t *testing.T) {
var tests = []struct {
first int
second int
result int
}{
{2, 2, 4},
{3, 2, 5},
}
t.Run("say something", func(t *testing.T) {
for _, test := range tests {
realSum := test.first + test.second
if realSum != test.result {
t.Error("wrong sum calculation")
}
}
});
}