Go入门笔记-10 测试

可以使用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:\Users\zgj\Desktop\GoStudy\Test>go test
PASS
ok      github.com/tarm/Test    1.424s

C:\Users\zgj\Desktop\GoStudy\Test>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参数会详细显示测试过程,如果与预期不一致。

 

 

  

  

posted @ 2021-07-25 21:30  zhaogaojian  阅读(54)  评论(0编辑  收藏  举报