Testify - Thou Shalt Write Tests
Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
Features include:
Get started:
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:
package yours
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSomething(t *testing.T) {
assert.Equal(t, 123, 123, "they should be equal")
assert.NotEqual(t, 123, 456, "they should not be equal")
assert.Nil(t, object)
if assert.NotNil(t, object) {
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:
package yours
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSomething(t *testing.T) {
assert := assert.New(t)
assert.Equal(123, 123, "they should be equal")
assert.NotEqual(123, 456, "they should not be equal")
assert.Nil(object)
if assert.NotNil(object) {
assert.Equal("Something", object.Value)
}
}
The require
package provides same global functions as the assert
package, but instead of returning a boolean result they terminate current test.
See t.FailNow for details.
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 setup expectations (testify) and assert that they indeed happened:
package yours
import (
"testing"
"github.com/stretchr/testify/mock"
)
type MyMockedObject struct{
mock.Mock
}
func (m *MyMockedObject) DoSomething(number int) (bool, error) {
args := m.Called(number)
return args.Bool(0), args.Error(1)
}
func TestSomething(t *testing.T) {
testObj := new(MyMockedObject)
testObj.On("DoSomething", 123).Return(true, nil)
targetFuncThatDoesSomethingWithObj(testObj)
testObj.AssertExpectations(t)
}
func TestSomethingElse(t *testing.T) {
testObj := new(MyMockedObject)
testObj.On("DoSomething", mock.Anything).Return(true, nil)
targetFuncThatDoesSomethingWithObj(testObj)
testObj.AssertExpectations(t)
}
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.
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:
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}
func (suite *ExampleTestSuite) TestExample() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
}
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:
import (
"testing"
"github.com/stretchr/testify/suite"
)
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}
func (suite *ExampleTestSuite) TestExample() {
suite.Equal(suite.VariableThatShouldStartAtFive, 5)
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}
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/mock
github.com/stretchr/testify/http
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!")
}
Staying up to date
To update Testify to the latest version, use go get -u github.com/stretchr/testify
.
Supported go versions
We support the three major Go versions, which are 1.8, 1.9 and 1.10 at the moment.
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.