Testing in Go (Golang): Frameworks and Tools
Go (Golang) is one of the few languages where testing is built into the standard library. Thanks to the built-in testing package, as well as powerful third-party tools, developers can build reliable and performant applications. In this article, we will cover the main frameworks and utilities for testing Go code: from simple unit tests to mocks and goroutine leak detection.
Built-in testing package
The testing package is the foundation of all testing in Go. Tests are written in files with the _test.go suffix. Test functions must have the signature func TestXxx(t *testing.T). Run them with the go test command.
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) }}For table-driven tests, a slice of structs is used:
func TestSumTable(t *testing.T) { tests := []struct { a, b, expected int }{ {1, 2, 3}, {0, 0, 0}, {-1, 1, 0}, } for _, tt := range tests { result := Sum(tt.a, tt.b) if result != tt.expected { t.Errorf("Sum(%d,%d) = %d; want %d", tt.a, tt.b, result, tt.expected) } }}Also supported are:
- Benchmarks — functions
func BenchmarkXxx(b *testing.B), run viago test -bench=. - Examples — functions
func ExampleXxx()for generating documentation and verifying output. - Subtests —
t.Run("name", func(t *testing.T) {...})for grouping and parallel execution.
Testify — Extended Capabilities
Testify is the most popular testing framework in Go. It adds convenient assertions, mocks, and a set of utilities.
Main Testify packages:
assert— checks with clear error messages.require— like assert, but stops the test on failure.mock— creating and verifying mock objects.suite— grouping tests into classes (TDD-style).
import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock")
// Example using assertfunc TestSumWithTestify(t *testing.T) { assert.Equal(t, 5, Sum(2, 3)) assert.NotEqual(t, 0, Sum(-1, 1))}
// Example mock with testify/mocktype Calculator interface { Add(a, b int) int}
type MockCalculator struct { mock.Mock}
func (m *MockCalculator) Add(a, b int) int { args := m.Called(a, b) return args.Int(0)}
func TestWithMock(t *testing.T) { mockCalc := new(MockCalculator) mockCalc.On("Add", 2, 3).Return(5)
result := mockCalc.Add(2, 3) assert.Equal(t, 5, result) mockCalc.AssertExpectations(t)}Gomock — Generating Mocks from Interfaces
Gomock (a fork of the original from Google) is a tool for automatically generating mocks based on interfaces. It is used together with the mockgen utility.
Installation and basic example:
// Installationgo install go.uber.org/mock/mockgen@latest
// Generate mock for an interface// mockgen -source=calculator.go -destination=mock_calculator.go -package=main
// Usage in a testfunc TestWithGomock(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()
mockCalc := NewMockCalculator(ctrl) mock Also in library
Working with Excel in PHP: PhpSpreadsheet, PhpOffice Libraries and XLSX Generation
Task Scheduler in C++: Building a Task Scheduler from Scratch
gRPC for JavaScript (Node.js): Libraries and Code Examples