// 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.
// 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) } }
mock package
The mock package allows the creation of mock objects. These objects can be used to attach expectations to method calls.
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:
func(db *MockDatabase) connect() error { // The args object holds the values that we should return for that call, the only thing left to do is return them. We use args.Error(0) to retrieve the first return argument of type error. If we didn’t know the type we could use args.Get(0). If we had multiple return arguments, we can retrieve each of them based on the index. For example: return args.Get(0), args.Error(1), for a method that returns a struct and an error. args := db.Called() return args.Error(0) }
funcTestSuccess(t *testing.T) { db := new(MockDatabase) message := "Hello"
// Set expectations db.On("connect").Return(nil) db.On("sendMessage", &message).Return(nil)
err := Talk(db, &message)
assert.Equal(t, nil, err, "No error") db.AssertExpectations(t) }
funcTestErrorOnConnect(t *testing.T) { db := new(MockDatabase)
// Set expectations db.On("connect").Return(errors.New("Some error"))
message := "Hello" err := Talk(db, &message)
assert.NotEqual(t, nil, err, "An error is thrown if connection fails") db.AssertExpectations(t) }
funcTestErrorOnMessage(t *testing.T) { db := new(MockDatabase) message := "Hello"
// Set expectations db.On("connect").Return(nil) db.On("sendMessage", &message).Return(errors.New("Some error"))
err := Talk(db, &message)
assert.NotEqual(t, nil, err, "An error is thrown if sendMessage fails") db.AssertExpectations(t) }
In the tests above, I also used db.AssertExpectations(t). This will fail if any of the expectations (set with db.On...) is not called. This is not necessary for all tests, and you might decide not to use it if your testing style is more loose.
suite package
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.
// 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 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 our example testing suite
For more information on writing suites, check out the API documentation for the suite package.
// 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 funcTestExampleTestSuite(t *testing.T) { suite.Run(t, new(ExampleTestSuite)) }