安装 github.com/stretchr/testify/assert
相比较goConvery, 使用者更多 star 更多
使用
- cal.go
package mainfunc Add(x int ) (result int) {result = x +2return result}
- cal_test.go
package mainimport ("testing""github.com/stretchr/testify/assert")func TestAdd(t *testing.T) {// assert equalityassert.Equal(t, Add(5), 7, "they should be equal")}
表驱动测试
package mainimport ("testing""github.com/stretchr/testify/assert")func TestAdd(t *testing.T) {// assert equalityassert.Equal(t, Add(5), 7, "they should be equal")}func TestCal(t *testing.T) {ass := assert.New(t)var tests = []struct {input intexpected int}{{2, 4},{-1, 1},{0, 2},{-5, -3},{999999997, 999999999},}for _, test := range tests {ass.Equal(Add(test.input), test.expected)}}
mock功能
- 使用 testify/mock 隔离第三方依赖或者复杂调用
- testfiy/mock 使得伪造对象的输入输出值可以在运行时决定
- https://github.com/euclidr/testingo
