测试框架testify使用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值