Using the Testing Package

Go includes a built-in testing package to write unit tests. A test function must start with Test and take *testing.T as a parameter.

func TestAdd(t *testing.T) {
  result := Add(2, 3)
  if result != 5 {
    t.Errorf("Expected 5, got %d", result)
  }
}

Run tests with go test.

← PrevNext →