Test conventions
Go testing rules:
- File ends in
_test.go - Function starts with
Test - Takes
*testing.T - Use
t.Log,t.Errorf,t.Fatalf
func TestSomething(t *testing.T) {}
go test ./... runs every test in the module.
Setup and teardown
Use t.Cleanup for teardown:
func TestDB(t *testing.T) {
db := setup()
t.Cleanup(func() { db.Close() })
// ...
}
t.Cleanup runs registered functions when the test ends, in reverse order. It replaces manual teardown.