testify
https://github.com/stretchr/testify
● 断言
原生测试框架里面缺失断言功能,在很多场景下都不方便,testify 提供的断言功能开箱即用,与原生测试框架完美契合:
func TestAssert(t *testing.T) {
assert := assert.New(t)
assert.Equal(123, 123, “they should be equal”)
assert.NotEqual(123, 456, “they should not be equal”)
o := make(map[string]string)
o[“ray”] = “jun”
if assert.NotNil(o) {
assert.Equal(“jun”, o[“ray”])
} else {
assert.Nil(o)
}
}
● Mock 能力
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(“Do