A toolkit with common assertions and mocks that plays nicely with the standard library
A toolkit with common assertions and mocks that plays nicely with the standard library
[!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 packageThe assert package provides some helpful methods that allow you to write better test code in Go.
See it in action:
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)
}
}testing.T object as the first argument. This is how it writes the errors out through the normal go test capabilities.if you assert many times, use the below:
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 packageThe 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 packageThe 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:
…To install Testify, use go get:
go get github.com/stretchr/testifyThis 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:
package yours
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSomething(t *testing.T) {
assert.True(t, true, "True is true!")
}To update Testify to the latest version, use go get -u github.com/stretchr/testify.
We currently support the most recent major Go versions from 1.19 onward.
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.
This project is licensed under the terms of the MIT license.
Eventually/Never panic on a nil condition, aborting the test binary
HttpSuccesss doesn't create Body -> error nil pointer dereference [Bug]
TearDownTest is being executed while parallel sub tests are still running
ElementsMatch should shows exact difference (per line)
Proposal: Global setting of context/size for `GetUnifiedDiffString`
mock: avoid copying call history in AssertNumberOfCalls
assert.Empty regression in v1.11.0: a pointer to an interface is never empty
mock.Call.Time documentation incomplete
mock: mock.Anything still works backwards
Add ErrorNotContains assertion