İleri
Ders 22 / 22
Testing
Go'da testler birinci sınıf vatandaştır. Dosya adı _test.go ile biter; komut go test.
go — mathx.go
package mathx
func Topla(a, b int) int { return a + b }go — mathx_test.go
package mathx
import "testing"
func TestTopla(t *testing.T) {
cases := []struct{ a, b, want int }{
{1, 2, 3},
{0, 0, 0},
{-1, 5, 4},
}
for _, c := range cases {
got := Topla(c.a, c.b)
if got != c.want {
t.Errorf("Topla(%d,%d)=%d; want %d", c.a, c.b, got, c.want)
}
}
}terminal
go test ./... go test -bench=. -benchmem
Alıştırma
Ortalama hesaplayan bir fonksiyon için table-driven test yaz.
Tebrikler! 22 derslik ana müfredatı tamamladın. Blog bölümünde konuları derinleştir.