Tests live in *_test.go files with func TestXxx(t *testing.T). Run with go test—built into the toolchain, unlike adding pytest or JUnit separately.
Table-driven tests
tests := []struct{ in, want int }{
{2, 4}, {3, 9},
}
for _, tt := range tests {
if got := sq(tt.in); got != tt.want { t.Fatalf(...) }
}
Subtests use t.Run for clearer failure output. Benchmarks use func BenchmarkXxx(b *testing.B).
Playground note
This lesson's editor runs main only—write full test files locally with go test ./... in CI.
Important interview questions and answers
- Q: Test file naming?
A:_test.gosuffix in same package (or_testexternal package for black-box tests). - Q: t.Fatal vs t.Error?
A: Fatal stops the test immediately; Error marks failure but continues.
Self-check
- What command runs tests?
- Why table-driven tests?
Tip: Table-driven tests with t.Run subtests scale well—name cases clearly for CI failure output.
Interview prep
- Test file naming?
*_test.goin same package or external test package.- Table-driven tests?
Slice of cases iterated in Test—clear failures and easy to extend.