1.编写代码

1)打卡GoLand,新建项目命名为gotest

2)在gotest目录下新建两个go file,如下图所示:

Go单元测试与报告

 

其中CircleArea.go为计算圆面积的待测go程序,代码如下:

package gotest

import "math"

func GetCircleArea(radius float32) float32 {
return math.Pi * radius * radius
}

CircleArea_test.go为测试用例程序,代码如下:
package gotest

import "testing"

//功能测试
/*
func TestGetCircleArea(t *testing.T) {
area := GetCircleArea(4.0)
if area != 50.265484 {
t.Error("测试未通过!")
} else {
t.Log("测试通过")
}
}*/

//压力测试
const N = 3

func TestGetCircleArea(t *testing.T) {
for i := 0; i < N; i++ {
area := GetCircleArea(4.0)
if area != 50.265484 {
t.Error("测试未通过!")
} else {
t.Log("测试通过")
}
}
}

2.执行测试

1)功能测试和压力测试

在终端执行命令:go test -v

Go单元测试与报告

相关文章: