可以使用Go实现自动化测试,EdgeX中也使用了大量的test代码
1、在工程里新建一个Test目录,创建一个文件testtestify_test.go,必须以_test结尾,代码如下
package Test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// 计算并返回 x + 2.
func Calculate(x int) (result int) {
result = x + 2
return result
}
func TestCalculate1(t *testing.T) {
assert.Equal(t, Calculate(2), 4)
}
func main() {
fmt.Println("Hello World")
}
func TestCalculate2(t *testing.T) {
assert := assert.New(t)
var tests = []struct {
input int
expected int
}{
{2, 4},
{-1, 1},
{0, 2},
{-5, -3},
{99999, 100001},
}
for _, test := range tests {
assert.Equal(Calculate(test.input), test.expected)
}
}
2、进入目录,执行
C:UserszgjDesktopGoStudyTest>go test PASS ok github.com/tarm/Test 1.424s C:UserszgjDesktopGoStudyTest>go test -v === RUN TestCalculate1 --- PASS: TestCalculate1 (0.00s) === RUN TestCalculate2 --- PASS: TestCalculate2 (0.00s) PASS ok github.com/tarm/Test 0.522s
3、带-v参数会详细显示测试过程,如果与预期不一致。
