Skip to content

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

License

Notifications You must be signed in to change notification settings

stretchr/testify

Repository files navigation

Testify - Thou Shalt Write Tests

ℹ️ We are working on testify v2 and would love to hear what you'd like to see in it, have your say here:https://cutt.ly/testify

Build StatusGo Report CardPkgGoDev

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

Features include:

Get started:

assertpackage

Theassertpackage 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:

packageyours

import(
"testing"
"github /stretchr/testify/assert"
)

funcTestSomething(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)
ifassert.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 thetesting.Tobject as the first argument. This is how it writes the errors out through the normalgo testcapabilities.
  • 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:

packageyours

import(
"testing"
"github /stretchr/testify/assert"
)

funcTestSomething(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)
ifassert.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)
}
}

requirepackage

Therequirepackage provides same global functions as theassertpackage, 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.

Seet.FailNowfor details.

mockpackage

Themockpackage 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 objecttestObj,can set up expectations (testify) and assert that they indeed happened:

packageyours

import(
"testing"
"github /stretchr/testify/mock"
)

/*
Test objects
*/

// MyMockedObject is a mocked object that implements an interface
// that describes an object that the code I am testing relies on.
typeMyMockedObjectstruct{
mock.Mock
}

// DoSomething is a method on MyMockedObject that implements some interface
// and just records the activity, and returns what the Mock object tells it to.
//
// In the real object, this method would do something useful, but since this
// is a mocked object - we're just going to stub it out.
//
// NOTE: This method is not being tested here, code that uses this object is.
func(m*MyMockedObject)DoSomething(numberint) (bool,error) {

args:=m.Called(number)
returnargs.Bool(0),args.Error(1)

}

/*
Actual test functions
*/

// TestSomething is an example of how to use our test object to
// make assertions about some target code we are testing.
funcTestSomething(t*testing.T) {

// create an instance of our test object
testObj:=new(MyMockedObject)

// set up expectations
testObj.On("DoSomething",123).Return(true,nil)

// call the code we are testing
targetFuncThatDoesSomethingWithObj(testObj)

// assert that the expectations were met
testObj.AssertExpectations(t)


}

// TestSomethingWithPlaceholder is a second example of how to use our test object to
// make assertions about some target code we are testing.
// This time using a placeholder. Placeholders might be used when the
// data being passed in is normally dynamically generated and cannot be
// predicted beforehand (eg. containing hashes that are time sensitive)
funcTestSomethingWithPlaceholder(t*testing.T) {

// create an instance of our test object
testObj:=new(MyMockedObject)

// set up expectations with a placeholder in the argument list
testObj.On("DoSomething",mock.Anything).Return(true,nil)

// call the code we are testing
targetFuncThatDoesSomethingWithObj(testObj)

// assert that the expectations were met
testObj.AssertExpectations(t)


}

// TestSomethingElse2 is a third example that shows how you can use
// the Unset method to cleanup handlers and then add new ones.
funcTestSomethingElse2(t*testing.T) {

// create an instance of our test object
testObj:=new(MyMockedObject)

// set up expectations with a placeholder in the argument list
mockCall:=testObj.On("DoSomething",mock.Anything).Return(true,nil)

// call the code we are testing
targetFuncThatDoesSomethingWithObj(testObj)

// assert that the expectations were met
testObj.AssertExpectations(t)

// remove the handler now so we can add another one that takes precedence
mockCall.Unset()

// return false now instead of true
testObj.On("DoSomething",mock.Anything).Return(false,nil)

testObj.AssertExpectations(t)
}

For more information on how to write mock code, check out theAPI documentation for themockpackage.

You can use themockery toolto autogenerate the mock code against an interface as well, making using mocks much quicker.

suitepackage

Thesuitepackage 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:

// Basic imports
import(
"testing"
"github /stretchr/testify/assert"
"github /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
typeExampleTestSuitestruct{
suite.Suite
VariableThatShouldStartAtFiveint
}

// 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
funcTestExampleTestSuite(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 ourexample testing suite

For more information on writing suites, check out theAPI documentation for thesuitepackage.

Suiteobject has assertion methods:

// Basic imports
import(
"testing"
"github /stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including assertion methods.
typeExampleTestSuitestruct{
suite.Suite
VariableThatShouldStartAtFiveint
}

// 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
funcTestExampleTestSuite(t*testing.T) {
suite.Run(t,new(ExampleTestSuite))
}

Installation

To install Testify, usego get:

go get github /stretchr/testify

This will then make the following packages available to you:

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

Import thetestify/assertpackage into your code using this template:

packageyours

import(
"testing"
"github /stretchr/testify/assert"
)

funcTestSomething(t*testing.T) {

assert.True(t,true,"True is true!")

}

Staying up to date

To update Testify to the latest version, usego get -u github /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 forCode generated withat the top of some files. Rungo generate./...to update generated files.

We also chat on theGophers Slackgroup in the#testifyand#testify-devchannels.


License

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