testify
Toolkit for Go tests with expressive assertions, fatal requires, mocks, and runnable suites.
Official Documentation ↗v1.9. Check the official docs for the latest API surface.Installation
go get github.com/stretchr/testifyMethods
No methods match this filter.
Toolkit for Go tests with expressive assertions, fatal requires, mocks, and runnable suites.
Overview
testify smooths Go test ergonomics: assert and require provide readable, diff-producing assertions with continue or abort semantics, mock verifies interaction protocols with expected calls, and suite groups tests around shared setup and teardown.
It complements rather than replaces the testing package: tests remain normal go test functions, and testify only upgrades the assertion and organization layer. It is the most widely adopted assertion toolkit in the Go ecosystem.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
assert— Records a failure and continues the test, producing diff-style messages for mismatches.require— Same API as assert but stops the test immediately, for preconditions that make the rest meaningless.mock.Mock— Declares expected calls and canned returns; assertions verify they happened.suite.Suite— Groups tests with shared SetupTest, TearDownTest, and stateful helpers.
Usage Notes
- require preconditions, assert outcomes; this one rule prevents most cascading failures.
- Call mock.AssertExpectations to fail on expected calls that never happened.
- Keep suites for integration groups; table tests remain the default for pure functions.
Common Pitfalls
- assert on an error then dereferencing the value panics; that path needed require.
- Forgetting m.Called inside the mocked method makes the mock hang or return zero values.
- Field comparison with assert.Equal is deep; floating-point values need InDelta.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/stretchr/testify@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:58.700Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- This is how it writes the errors out through the normal go test capabilities. (https://cdn.jsdelivr.net/gh/stretchr/testify@latest/README.md)
FAQ
- assert or require, which by default?
- Use require for preconditions, a nil error before reading the result, and assert for everything after. Continuing after a failed precondition produces cascading nil panics that bury the real failure.
- How do mocks differ from hand-written fakes?
- Mocks via testify record calls and assert they happened, which suits verifying interaction protocols. Hand-written fakes suit stateful behavior and are clearer when the stub grows logic; prefer fakes when the mock setup starts to read like an implementation.
- Do suites replace table-driven tests?
- No. Table-driven cases remain the Go pattern for one behavior across inputs. Suites add shared lifecycle and state for integration-style groups. Both coexist in one package without conflict.
Report a problem with this API docOpens a pre-filled issue on GitHub.