Testing in Go (Golang): frameworks and tools
Go (Golang) was designed from the start with modern development needs in mind, so the standard library already includes a powerful testing package. However, complex projects often require additional tools: mocks, assertions, BDD-style testing, and goroutine leak detection. In this article, we will cover the main frameworks and libraries for testing in Go, show code examples, and explain when to use what.
1. Built-in testing package
The standard testing package is the foundation of everything. Tests are written in files with the _test.go suffix, and functions must have the signature func TestXxx(t *testing.T). Example:
package main
import ( "testing")
func Sum(a, b int) int { return a + b}
func TestSum(t *testing.T) { result := Sum(2, 3) expected := 5 if result != expected { t.Errorf("Sum(2,3) = %d; want %d", result, expected) }}Running tests: go test ./.... For benchmarks, use testing.B, for examples — ExampleXxx.
2. testify library
Testify is the most popular third-party library for testing in Go. It provides convenient assertions, mocks, and helper functions.
Installation:
go get github.com/stretchr/testifyExample with assertions:
package main
import ( "testing" "github.com/stretchr/testify/assert")
func TestSumWithTestify(t *testing.T) { result := Sum(2, 3) assert.Equal(t, 5, result, "Sum should be 5") assert.NotEqual(t, 0, result) assert.True(t, result > 0)}Testify also includes the mock package for creating mocks. Example:
package main
import ( "testing" "github.com/stretchr/testify/mock")
// DataService — interface we will mocktype DataService interface { GetData(id int) string}
// MockDataService — mock structuretype MockDataService struct { mock.Mock}
func (m *MockDataService) GetData(id int) string { args := m.Called(id) return args.String(0)}
func TestGetData(t *testing.T) { mockService := new(MockDataService) mockService.On("GetData", 1).Return("result1")
result := mockService.GetData(1) assert.Equal(t, "result1", result) mockService.AssertExpectations(t)}3. gomock — mock generation
Gomock from Google is a framework for generating mocks based on interfaces. It works through the mockgen utility.
Installation:
go install github.com/golang/mock/mockgen@v1.6.0Usage example:
//go:generate mockgen -source=service.go -destination=mock_service.go -package=main
type Service interface { Process(data string) error}
// In test:func TestProcess(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()
mockService := NewMockService(ctrl) mockService.EXPECT().Process("test").Return(nil)
err := mockService.Process("test") assert.NoError(t, err)}Gomock is good for large projects with many interfaces — code generation saves time.
4. ginkgo — BDD-style
Ginkgo is a framework for Behaviour-Driven Development (BDD). It allows writing tests in a "Describe", "Context", and "It" style.
Installation:
go get github.com/onsi/ginkgo/v2go get github.com/onsi/gomegaExample:
package main
import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" "testing")
func TestCalculator(