Favicon of Testify

Testify

Testify is a Go toolkit with assert, require, mock, and suite packages that extend the standard testing package.

Testify website screenshot
Testify GitHub repository preview

Testify is a Go library that adds assertions, mocking, and test-suite structure on top of the standard testing package. It targets Go developers who want more readable test failures and less boilerplate than raw if checks against t.Error or t.Fatal, without pulling in a full replacement test runner.

The library ships as four main packages: assert, require, mock, and suite. Each works with go test directly, so there's no separate test binary or runner to learn.

Key features

  • assert package: provides functions like Equal, NotEqual, and Nil that print readable failure messages and return a bool so you can branch on assertion results.
  • require package: mirrors the assert API but calls t.FailNow() on failure, stopping the test immediately instead of continuing with a false assumption.
  • mock package: lets you build mock objects by embedding mock.Mock, setting up expectations with On() and Return(), and verifying calls with AssertExpectations.
  • suite package: supports struct-based test suites with SetupTest/TearDownTest hooks, useful for grouping related tests that share setup logic.
  • Object-oriented assertions: assert.New(t) and suite embedding give you assertion methods without repeating the t argument on every call.
  • testifylint integration: a companion linter (run via golangci-lint) catches common misuse of the assertion functions.
  • Mock code generation: works with the external mockery tool to autogenerate mock implementations from interfaces.

Ideal use cases

Testify fits projects already using Go's standard testing package that want clearer failure output and reusable assertion helpers, rather than a full BDD framework. It's a good match for tests that need mock objects for interfaces (external services, repositories, clients) without hand-writing every mock method's bookkeping. Suites help when you have multiple tests sharing setup/teardown logic, such as spinning up a test database connection once per suite run.

It's not a fit if you need parallel test execution within a suite. The suite package doesn't support t.Parallel(), per an open issue in the repo. If your project needs a full mocking framework with automatic mock generation baked in, you'll still want to pair testify with mockery or write mocks by hand. And if you prefer table-driven tests with plain standard-library assertions and no extra dependency, testify adds a package you may not need.

Installation

Install with go get:

go get github.com/stretchr/testify

This makes the following packages available:

github.com/stretchr/testify/assert
github.com/stretchr/testify/require
github.com/stretchr/testify/mock
github.com/stretchr/testify/suite
github.com/stretchr/testify/http (deprecated)

Import assert in a test file:

package yours

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {
	assert.True(t, true, "True is true!")
}

To update to the latest version later:

go get -u github.com/stretchr/testify

The project supports the most recent major Go versions from 1.19 onward.

Categories:

Frequently asked questions

Share:

Stars
26.1K
Forks
1.8K
Last commit
6 days ago
Repository age
14 years
License
MIT
Self-hosted
No
Activity score
82/100
View Repository
Built with:
Ad
Favicon

 

  
 

Similar to Testify

Favicon

 

  
 
Favicon

 

  
 
Favicon