Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

testify

> 测试质量
Open source

A toolkit with common assertions and mocks that plays nicely with the standard library

26.1K stars0 likes0 views
WebsiteGitHub

About

A toolkit with common assertions and mocks that plays nicely with the standard library

Testify - Thou Shalt Write Tests

[!NOTE] Testify is being maintained at v1, no breaking changes will be accepted in this repo.
See discussion about v2.

Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.

Features include:

  • Easy assertions
  • Mocking
  • Testing suite interfaces and functions

Get started:

  • Install testify with one line of code, or update it with another
  • For an introduction to writing test code in Go, see https://go.dev/doc/code#Testing
  • Check out the API Documentation https://pkg.go.dev/github.com/stretchr/testify
  • Use testifylint (via golangci-lint) to avoid common mistakes
  • A little about Test-Driven Development (TDD)

assert package

The assert package provides some helpful methods that allow you to write better test code in Go.

  • Prints friendly, easy to read failure descriptions
  • Allows for very readable code
  • Optionally annotate each assertion with a message

See it in action:

go
package yours

import (
	"testing"

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

func TestSomething(t *testing.T) {
	// assert equality
	assert.Equal(t, 123, 123, "they should be equal")

	// assert inequality
	assert.NotEqual(t, 123, 456, "they should not be equal")

	// assert for nil (good for errors)
	assert.Nil(t, object)

	// assert for not nil (good when you expect something)
	if assert.NotNil(t, object) {
		// now we know that object isn't nil, we are safe to make
		// further assertions without causing any errors
		assert.Equal(t, "Something", object.Value)
	}
}
  • Every assert func takes the testing.T object as the first argument. This is how it writes the errors out through the normal go test capabilities.
  • Every assert func returns a bool indicating whether the assertion was successful or not, this is useful for if you want to go on making further assertions under certain conditions.

if you assert many times, use the below:

go
package yours

import (
	"testing"

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

func TestSomething(t *testing.T) {
	assert := assert.New(t)

	// assert equality
	assert.Equal(123, 123, "they should be equal")

	// assert inequality
	assert.NotEqual(123, 456, "they should not be equal")

	// assert for nil (good for errors)
	assert.Nil(object)

	// assert for not nil (good when you expect something)
	if assert.NotNil(object) {
		// now we know that object isn't nil, we are safe to make
		// further assertions without causing any errors
		assert.Equal("Something", object.Value)
	}
}

require package

The require package provides same global functions as the assert package, but instead of returning a boolean result they terminate current test. These functions must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Otherwise race conditions may occur.

See t.FailNow for details.

mock package

The mock package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.

An example test function that tests a piece of code that relies on an external object testObj, can set up expectations (testify) and assert that they indeed happened:

…

For more information on how to write mock code, check out the API documentation for the mock package.

You can use the mockery tool to autogenerate the mock code against an interface as well, making using mocks much quicker.

suite package

[!WARNING] The suite package does not support parallel tests. See #934.

The suite package provides functionality that you might be used to from more common object-oriented languages. With it, you can build a testing suite as a struct, build setup/teardown methods and testing methods on your struct, and run them with 'go test' as per normal.

An example suite is shown below:

…

For a more complete example, using all of the functionality provided by the suite package, look at our example testing suite

For more information on writing suites, check out the API documentation for the suite package.

Suite object has assertion methods:

…

Installation

To install Testify, use go get:

go get github.com/stretchr/testify

This will then make the following packages available to you:

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 the testify/assert package into your code using this template:

go
package yours

import (
	"testing"

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

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

Staying up to date

To update Testify to the latest version, use go get -u github.com/stretchr/testify.


Supported go versions

We currently support the most recent major Go versions from 1.19 onward.


Contributing

Please feel free to submit issues, fork the repository and send pull requests!

When submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra credit for those using Testify to write the test code that demonstrates it.

Code generation is used. Look for Code generated with at the top of some files. Run go generate ./... to update generated files.

We also chat on the Gophers Slack group in the #testify and #testify-dev channels.


License

This project is licensed under the terms of the MIT license.

Issues· 386 open

View all issuesOpen on GitHub
  • #1970

    Eventually/Never panic on a nil condition, aborting the test binary

    Updated Sep 27, 2026
  • #810

    HttpSuccesss doesn't create Body -> error nil pointer dereference [Bug]

    pkg-httpUpdated Sep 23, 2026
  • #934

    TearDownTest is being executed while parallel sub tests are still running

    bugUpdated Sep 22, 2026
  • #1376

    ElementsMatch should shows exact difference (per line)

    enhancementpkg-assertUpdated Sep 9, 2026
  • #1878

    Proposal: Global setting of context/size for `GetUnifiedDiffString`

    enhancementenhancement: output formatUpdated Sep 7, 2026
  • #1957

    mock: avoid copying call history in AssertNumberOfCalls

    Updated Sep 5, 2026
  • #1955

    assert.Empty regression in v1.11.0: a pointer to an interface is never empty

    bugUpdated Sep 4, 2026
  • #1947

    mock.Call.Time documentation incomplete

    bugUpdated Aug 31, 2026
  • #1943

    mock: mock.Anything still works backwards

    bugUpdated Aug 24, 2026
  • #1940

    Add ErrorNotContains assertion

    enhancementUpdated Aug 22, 2026

> Tags

Goassertionsgogolangmocking

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 30, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.