参考: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 根据覆盖率从数据直接生成覆盖率页面