Testing in Go (Golang): Frameworks and Tools — Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Testing in Go (Golang): Frameworks and Tools



Testing is an integral part of developing reliable software. The Go language (Golang) was designed from the outset with testing needs in mind: the standard library includes a powerful testing package, and a rich ecosystem of additional tools has grown around it. In this article, we will thoroughly examine the main frameworks and tools for testing in Go, including writing unit tests, using mocks, the BDD approach, and benchmarks.



1. Standard testing package



The core of testing in Go is the testing package. It provides a minimalistic but very effective set of functions. Tests are written in files with the _test.go suffix and are regular functions with the signature func TestXxx(t *testing.T).



Example of a simple test for an addition function:



package main

import "testing"

func Add(a, b int) int { return a + b}

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


Tests are run with the command:



go test ./...


The testing package also supports:

  • Table-driven tests — for checking multiple sets of input data.
  • Subtests (using t.Run) — for grouping and parallel execution.
  • Benchmarks — functions of the form func BenchmarkXxx(b *testing.B).
  • Examples — functions func ExampleXxx(), which also serve as documentation.


2. Testify — Extending Testing Capabilities



Testify is one of the most popular third-party libraries for testing in Go. It extends the standard testing package by providing:

  • assert — convenient functions for checking conditions with clear error messages.
  • require — same as assert, but stops test execution on failure.
  • suite — the ability to structure tests as a set (a struct with methods).
  • mock — a simple framework for creating mock objects.


Example of using assert and suite:



package main

import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite")

type MySuite struct { suite.Suite Value int}

func (s *MySuite) SetupTest() { s.Value = 10}

func (s *MySuite) TestValue() { assert.Equal(s.T(), 10, s.Value, "Value should be 10")}

func TestMySuite(t *testing.T) { suite.Run(t, new(MySuite))}


Testify makes test code more readable and reduces the amount of boilerplate code.



3. GoMock — Mock Generation



GoMock is a framework for creating mock objects based on interfaces. It consists of two parts:

  • mockgen — a utility for generating mock code.
  • gomock — a package for using mocks in tests.


Example of using GoMock:



  1. Define the interface:


package main

type UserService interface { GetUser(id int) (string, error)}


  1. Generate the mock:


mockgen -source=user.go -destination=mock_user.go -package=main


  1. Use in a test:


func TestGetUserName(t *testing.T) {    ctrl := gomock.NewController(t)    defer ctrl.Finish()

mockService := NewMockUserService(ctrl) mockService.EXPECT().GetUser(1).Return("Alice", nil)

name,

Recommendations