go代码覆盖测试

go代码覆盖测试

(金庆的专栏 2020.2)

假设应用名为 MyMod, 主函数的文件为 main.go, 那就创建如下的 main_test.go 文件:

package main

/* 测试整个服务器。
	go test -c -covermode=count -coverpkg ./...
生成 MyMod.test.exe. 复制到 bin 目录下运行:
	MyMod.test.exe --systemTest --test.coverprofile MyMod.cov
生成代码覆盖测试结果 MyMod.cov, 需要在 go.mod 管理的目录下执行
	go tool cover -html=MyMod.cov -o MyMod.html
打开 MyMod.html 查看结果。
*/

import (
	"flag"
	"fmt"
	"testing"
)

var systemTest *bool

func init() {
	systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
}

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
	if *systemTest {
		fmt.Println("Test system...")
		main()
	}
}

MyMod.cov 会积累,可加入 Git.
MyMod.cov 提交前,需对其排序,方便查看 diff.

排序脚本 sort_cov.bat 如下:

head -1 MyMod.cov > tmp.cov
cat MyMod.cov | sed '1d' | sort >> tmp.cov
cp tmp.cov MyMod.cov
del tmp.cov
pause
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值