SkillsApr 13, 2026·3 min read

Testify — Go Testing Toolkit with Assertions and Mocks

Testify is the most popular Go testing toolkit. It provides expressive assertions (assert/require), powerful mocking, and test suites with setup/teardown — making Go tests more readable and maintainable than the standard library alone.

Agent ready

Ready-to-run agent install

This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Established
Entrypoint
step-1.md
Direct install command
npx -y tokrepo@latest install 1056f32f-373d-11f1-9bc6-00163e2b0d79 --target codex

Run after dry-run confirms the install plan.

TL;DR
Testify adds expressive assertions, a mocking framework, and test suite support to Go's built-in testing package.
§01

What it is

Testify is the most widely used Go testing toolkit. It extends Go's standard testing package with three components: assert (soft assertions that log failures and continue), require (hard assertions that stop the test), and mock (interface mocking with expectations and verifications).

It targets Go developers who find the standard if got != want { t.Errorf(...) } pattern too verbose. Testify makes tests more readable and reduces boilerplate without replacing Go's test runner.

§02

How it saves time or tokens

A typical Go test without Testify requires 3-4 lines per assertion (check, format error message, call t.Errorf). Testify reduces this to one line: assert.Equal(t, expected, actual). For a test file with 50 assertions, this saves 100-150 lines of boilerplate.

The mock package generates mock implementations from interfaces, eliminating the need to write manual mock structs.

§03

How to use

  1. Install Testify:
go get github.com/stretchr/testify
  1. Write assertions:
package mypackage

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

func TestUser(t *testing.T) {
    user, err := GetUser(1)
    require.NoError(t, err)          // stops test if err != nil
    assert.Equal(t, "Alice", user.Name)
    assert.NotEmpty(t, user.Email)
    assert.Contains(t, user.Roles, "admin")
}
  1. Use mocks for interface dependencies:
import "github.com/stretchr/testify/mock"

type MockDB struct { mock.Mock }

func (m *MockDB) GetUser(id int) (*User, error) {
    args := m.Called(id)
    return args.Get(0).(*User), args.Error(1)
}
§04

Example

// Test suite with setup/teardown
package mypackage

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

type UserSuite struct {
    suite.Suite
    db *TestDB
}

func (s *UserSuite) SetupTest() {
    s.db = NewTestDB()
    s.db.Seed()
}

func (s *UserSuite) TearDownTest() {
    s.db.Clean()
}

func (s *UserSuite) TestCreateUser() {
    user, err := s.db.CreateUser("Charlie")
    s.Require().NoError(err)
    s.Equal("Charlie", user.Name)
}

func TestUserSuite(t *testing.T) {
    suite.Run(t, new(UserSuite))
}
§05

Related on TokRepo

§06

Common pitfalls

  • Use require for preconditions (like checking errors) and assert for value checks. Using assert for error checks means the test continues with a nil pointer.
  • Testify mock expectations must be verified with mock.AssertExpectations(t) at the end of the test. Forgetting this means unmet expectations pass silently.
  • Do not import both assert and require under the same alias. Use their package names as-is to keep test code clear.

Frequently Asked Questions

Does Testify replace go test?+

No. Testify extends the standard testing package. You still run tests with `go test`. Testify provides better assertions and mocks but uses Go's built-in test runner.

What is the difference between assert and require?+

assert logs a failure and continues the test. require logs a failure and stops the test immediately via t.FailNow(). Use require for preconditions where continuing would cause panics (like nil error checks).

Can I generate mocks automatically?+

Yes. Use the mockery tool (`go install github.com/vektra/mockery/v2@latest`) to generate Testify mock implementations from Go interfaces automatically.

Is Testify the standard in Go?+

Testify is the most popular third-party testing library in the Go ecosystem. Many Go projects use it, though some teams prefer the standard library approach or alternative libraries like goconvey.

Does Testify support table-driven tests?+

Testify works well with Go's table-driven test pattern. Use assert or require inside the loop body of a table-driven test with t.Run() subtests for clear output.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets