安装 github.com/stretchr/testify/assert
相比较goConvery, 使用者更多 star 更多
使用
- cal.go
package main
func Add(x int ) (result int) {
result = x +2
return result
}
- cal_test.go
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
// assert equality
assert.Equal(t, Add(5), 7, "they should be equal")
}
表驱动测试
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
// assert equality
assert.Equal(t, Add(5), 7, "they should be equal")
}
func TestCal(t *testing.T) {
ass := assert.New(t)
var tests = []struct {
input int
expected 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