Writing Unit Tests with testing
TestXxx functions, t.Error, t.Fatal, t.Helper
testing package
Go's built-in testing package provides everything needed for unit tests: test functions, failure reporting, and test binaries. No external framework is required.
Test function signature
Test functions must start with Test, take *testing.T, and live in files ending with _test.go.
func TestAdd(t *testing.T) {
got := Add(2, 3)
if got != 5 {
t.Errorf("Add(2,3) = %d; want 5", got)
}
}All lessons in this course
- Writing Unit Tests with testing
- Table-Driven Tests
- Test Doubles: Mocks and Stubs
- Test Coverage and testify