go服务统计覆盖率

参考:https://www.cnblogs.com/zhaoxd07/p/8028847.html

二进制文件server_test

1、创建main函数的test文件,不需要做任何改动
main_test.go

package main
 
// This file is mandatory as otherwise the packetbeat.test binary is not generated correctly.
 
import (
    "flag"
    "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 {
        main()
    }
}

2、生成server_test.test文件

在生成server_test 二进制文件的后面 加入:

go test -c -covermode=count -coverpkg ./models/rec,./models/poi,./naming/acc' ./build.sh

-covermode=count 表示 生成的二进制中包含覆盖率计数信息
-coverpkg 后面是要统计覆盖率的文件源码

3、 启动服务,在启动命令后加参数:

nohup ./server_test.test -systemTest -test.coverprofile ./total_coverage.cov  >./test.log 2>&1 &

-systemTest 用来启动前面说过的main test
-test.coverprofile 用来指定覆盖率信息写入到哪个文件

4、服务启动之后,执行自动化case

5、停止服务后自动生成total_coverage.cov文件

pid_server=`ps -ef | grep server_test | grep -v "grep" | awk '{print $2}'`
kill $pid_server

6、分析total_coverage.cov文件, 生成total_coverage.html文件

go tool cover -func=./total_coverage.cov -o total_coverage.txt
go tool cover -html=./total_coverage.cov -o ./total_coverage.html

total_coverage.cov 覆盖率相关数据
total_coverage.txt 统计覆盖率相关数据
total_coverage.html 根据覆盖率从数据直接生成覆盖率页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值