Esta página se muestra en inglés. Una traducción al español está en curso.
SkillsApr 13, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
step-1.md
Comando de instalación directa
npx -y tokrepo@latest install 1056f32f-373d-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

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.

Preguntas frecuentes

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.

Referencias (3)

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados