Skills2026年4月13日·1 分钟阅读

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 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install 1056f32f-373d-11f1-9bc6-00163e2b0d79 --target codex

先 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.

常见问题

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.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产