testify Documentation

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

26,095 stars Go Go 1 file ยท ~2,901 tokens #assertions#go#golang#mocking#testify#testing#toolkit
RAW Doc

1. Project Overview & Quickstart (stretchr/testify)

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:

Get started:

[`assert`](https://pkg.go.dev/github.com/stretchr/testify/assert "API documentation") 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`](https://pkg.go.dev/github.com/stretchr/testify/require "API documentation") 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`](https://pkg.go.dev/github.com/stretchr/testify/mock "API documentation") 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:

text
/* Detailed source-code truncated for AI context efficiency. */

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`](https://pkg.go.dev/github.com/stretchr/testify/suite "API documentation") 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:

go
// Basic imports
import (
	"testing"

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

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type ExampleTestSuite struct {
	suite.Suite
	VariableThatShouldStartAtFive int
}

// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
	suite.VariableThatShouldStartAtFive = 5
}

// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
	assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
	suite.Run(t, new(ExampleTestSuite))
}

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:

go
// Basic imports
import (
	"testing"

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

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including assertion methods.
type ExampleTestSuite struct {
	suite.Suite
	VariableThatShouldStartAtFive int
}

// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
	suite.VariableThatShouldStartAtFive = 5
}

// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
	suite.Equal(suite.VariableThatShouldStartAtFive, 5)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
	suite.Run(t, new(ExampleTestSuite))
}

Installation

To install Testify, use go get:

text
go get github.com/stretchr/testify

This will then make the following packages available to you:

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

2. Official Technical Reference & Guides (stretchr/docs)

Stretchr Documentation

Stretchr is a schemaless datastack designed to let you stop worrying about your data and start deciding what to do with it. It replaces the need for most backend and API development by providing a powerful query interface, RESTful API and users with fine-grained permissions.

Why Stretchr?

Features With Stretchr Without Stretchr
Nested Relations Stretchr provides nested relationships between data. For example, a todo resource could have a comment located at /todos/1/comments/1. That same comment is accessible at /comments/1 Traditionally, acheiving this kind of data flexibility in NoSQL is handled by duplicating data
Querying Stretchr provides complex querying capabilities to client applications. Learn more about specific querying options on the querying page. Without Stretchr, you won't want to expose this level of flexibility to the world without dedicating a backend application to protect it
Users Oauth solutions are built in, adding support for users is usually just a single line of code. Oauth implementations require custom backend development and must be handled explicitely for each oauth provider.
Permissions Permissions in Stretchr can be customized per app, user or down to an individual data resource. The flexibility lets you define just about any level of access you need. Without Stretchr, you'll be implementing your own permissions system in your backend layer. As the needs of your application evolve, these permissions will need to be updated to support new functionality.
Integration Stretchr provides a variety of open source SDKs that let you get started pushing and pulling data immediately. No need to create databases or collections. Without Stretchr, you'll be looking at a full installation of a NoSQL solution, and then finding an adapter to work with. You definitely won't find any front end JS or Cocoa SDKs, we promise :)

Getting Started

The simplest way to get started is to try out our multitenant cloud environment. To sign up, head on over to stretchr.com. If you're interested in learning how Stretchr can be deployed inside your organization, get in touch on our enterprise page.