Go nduweni testing built-in liwat paket testing lan perintah go test — ora perlu framework eksternal. Tests manggon ing file _test.go, lan Go ngutamakake table-driven tests, benchmarks, lan tooling sing apik minangka fitur first-class.
Go nduweni testing built-in liwat paket testing lan perintah go test — ora perlu framework eksternal. Tests manggon ing file _test.go, lan Go ngutamakake table-driven tests, benchmarks, lan tooling sing apik minangka fitur first-class.
// math.go
func Add(a, b int) int { return a + b }
// math_test.go — same package, _test.go suffix
import "testing"
func TestAdd(t *testing.T) { // Test prefix + *testing.T parameter
result := Add(2, 3)
if result != 5 {
t.Errorf("Add(2,3) = %d; want 5", result) // report failure (continues)
// t.Fatalf stops the test immediately
}
}
// run with: go test (or `go test -v` for verbose, `go test ./...` for all packages)
Fungsi test diwiwiti karo Test, njupuk *testing.T, lan nglaporake kegagalan liwat t.Errorf/t.Fatalf. Ora ana keyword assertion — sampeyan mriksa kondisi karo if biasa lan nglaporake kegagalan (Go luwih seneng gaya sing eksplisit iki).
func TestAdd(t *testing.T) {
tests := []struct { // a table of test CASES
name string
a, b int
expected int
}{
{"positives", 2, 3, 5},
{"with zero", 0, 5, 5},
{"negatives", -1, -1, -2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { // a SUBTEST per case (named, isolated)
if got := Add(tt.a, tt.b); got != tt.expected {
t.Errorf("Add(%d,%d) = %d; want %d", tt.a, tt.b, got, tt.expected)
}
})
}
}
Table-driven tests minangka idiom Go sing dominan — nggawe slice kasus, loop over karo t.Run subtests. Iki nggawe nambah kasus jadi gampang lan menehi reporting pass/fail sing jelas lan individual per kasus.
func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ { // the framework chooses b.N for statistical accuracy
Add(2, 3)
}
}
// go test -bench=. → reports ns/op (and -benchmem for allocations)
go test -cover # coverage percentage
go test -coverprofile=c.out # then: go tool cover -html=c.out (visual report)
go test -race # the RACE DETECTOR — finds data races (crucial for concurrency)
go test -run TestAdd # run specific tests
Flag -race arang penting — ndeteksi data races ing kode concurrent, alat kunci amarga fokus concurrency Go.
func TestMain(m *testing.M) { // package-level setup/teardown
setup()
code := m.Run()
teardown()
os.Exit(code)
}
// t.Helper() marks a function as a test helper (better failure line reporting)
// httptest package for testing HTTP handlers; interfaces for mocking dependencies
Testing minangka bagian first-class, built-in Go — paket testing lan go test ora mbutuhake framework eksternal, ngrefleksikake enfase Go ing testing minangka praktik inti.
Nembe konvensi iku essential everyday knowledge: file _test.go lan struktur TestXxx(t *testing.T), gaya failure-reporting sing eksplisit (tanpa assertion), lan utamané pola table-driven test sing dominan idiomatic Go (nyakup akeh kasus kanthi compact karo reporting per-kasus sing jelas liwat subtests).
Tuduh penting uga benchmarks Go built-in (performa minangka testable concern) lan tooling sing kuat — analisis coverage lan utamané detector -race, sing nemokake data races ing kode concurrent (vital amarga panggunaan concurrency-heavy Go).
Nembe carane nulis tests effective lan idiomatic Go, gunakaké race detector, lan leverage httptest/interfaces kanggo ngetèst komponen nyata minangka hallmark professional Go development lan topik sing asring ngrefleksikake code-quality fluency.
Pustaka pitakon wawancara IT kanthi jawaban rinci — saka Junior nganti Senior.
Nyumbang