Golang单测

表格驱动测试:

tests := []struct{
	a, b, c int32
}{
	{1, 2, 3},
	{0, 2, 2},
	{1, 3, 4},
	{math.MaxInt32, 1, math.MinInt32},
}
for _, test := range tests {
	if actual := add(test.a, test.b); actual!= test.c{
		//todo
	}
}
.......

写一个简单的测试用例

需要被测试得代码:

package basic

import (
	"fmt"
	"math"
)
func tryTriangle(a, b int) int{
	var c int
	c = int(math.Sqrt(float64(a*a + b*b)))
	return c
}

func triangle(){
	var a, b int = 3, 4
	fmt.Println(tryTriangle(a, b))
}

测试代码:

package basic

import "testing"

func TestTriangle(t *testing.T){
	tests := []struct{a, b, c int} {
		{3, 4, 5},
		{5, 12, 13},
		{8, 15, 17},
	}
	for _, tt := range tests {
		if actual := tryTriangle(tt.a, tt.b); actual != tt.c {
			t.Errorf("tryTriangle(%d, %d);" + "got %d no %d", tt.a, tt.b, actual, tt.c)
		}
	}
}

验证代码覆盖率使用Goland工具 截图图下:
image-20220302224959264.png

测算运行速度的单测:
image-20220302225806393.png

如何通过命令进行单侧:
image-20220302225114308.png

通过 go tool cover -html=c.out 来查看覆盖率

测试程序耗时位置:

// 生成性能数据文件
go test -bench . -cpuprofile cpu.out
//打开cpu.out文件 查看性能数据
go tool pprof cpu.out
	web
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值