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参数会详细显示测试过程,如果与预期不一致。

本博客是个人工作中记录,更深层次的问题可以提供有偿技术支持。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。
另外建了几个QQ技术群:
2、全栈技术群:616945527
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600
闲置域名WWW.EXAI.CN (超级人工智能)出售。

浙公网安备 33010602011771号