Testify for Go: Complete Guide with Examples

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

What is Testify and why is it needed?



Testify is one of the most popular testing libraries for the Go language. It extends the capabilities of the standard testing package, providing developers with powerful utilities for writing clean, readable, and maintainable tests. Testify solves several key problems that Go developers face:



  • Simplified assertions: Instead of cumbersome constructs with if and t.Errorf, Testify offers concise methods like assert.Equal.
  • Error readability: When a test fails, Testify outputs detailed messages showing expected and actual values.
  • Mock creation: Built-in mock support allows you to easily isolate the code under test from external dependencies (databases, HTTP clients, file systems).
  • Test suites: Allows grouping tests into logical groups with shared setup/teardown configurations.


Testify is ideal for projects of any size — from small utilities to large microservice architectures. It is the de facto standard for testing in the Go community.



Installation



Testify is installed as a regular Go dependency. Make sure you have Go installed (version 1.16 or higher) and a module initialized (go mod init).



go get github.com/stretchr/testify


After running the command, Testify will be added to your go.mod file. You can now import it into your test files.



Quick Start



Let's write a simple test for a function that adds two numbers. Create a file math.go:



package main

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


Now create a test file math_test.go:



package main

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

func TestAdd(t *testing.T) { result := Add(2, 3) expected := 5 assert.Equal(t, expected, result, "2 + 3 should equal 5")}


Run the test:



go test -v


You will see a green result. If you change the expected value to 6, the test will fail with a detailed error message.



Main Methods and Functions



1. assert.Equal

Description: Checks that two values are equal. Uses reflect.DeepEqual for comparing complex types (slices, structs, maps).

Parameters:

  • t — *testing.T object
  • expected — expected value
  • actual — actual value
  • msg (optional) — error message

Example:

func TestEqual(t *testing.T) {    assert.Equal(t, 10, 5+5, "5+5 should be 10")    assert.Equal(t, []int{1, 2, 3}, []int{1, 2, 3})}


2. assert.NotEqual

Description: The opposite of Equal. Checks that two values are NOT equal.

Parameters: Same as Equal.

Example:

func TestNotEqual(t *testing.T) {    assert.NotEqual(t, 10, 5+4, "5+4 does not equal 10")}


3. assert.True / assert.False

Description: Checks that an expression is true (True) or false (False).

Parameters:

  • t — *testing.T object
  • condition — boolean expression
  • msg (optional) — error message

Example:

func TestTrueFalse(t *testing.T) {    assert.True(t, 5 > 3, "5                    

Recommendations