0PricingLogin
Go Academy · Lesson

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

  1. Writing Unit Tests with testing
  2. Table-Driven Tests
  3. Test Doubles: Mocks and Stubs
  4. Test Coverage and testify
← Back to Go Academy