Golang 全量覆盖率统计

实践项目:趣头条-实惠喵后端

单元测试覆盖率统计

待整理。。

功能测试覆盖率统计

1.创建main_test.go文件,和mian.go文件在同级目录,文件内容如下:

package main
 
import (
   "os"
   "os/signal"
   "syscall"
   "testing"
)
 
func TestServer(t *testing.T) {
   ch := make(chan os.Signal, 1)
   signal.Notify(ch, syscall.SIGUSR1)
   go main()
   <-ch
}

2.生成可执行的二进制服务文件

# go test -c -cover -covermode=count -o "my_test_binary" -coverpkg ./...

3.启动服务

# ./my_test_binary -test.coverprofile coverage.cov -test.run=TestServer > my_test_binary .log 2>&1

4.运行测试,最后使用kill -USR1 $PID停止服务,以收集覆盖率数据,目录中会生成coverage.cov覆盖率信息文件

5.转化.cov文件
直接在窗口中展示覆盖率报告:

go tool cover -func=coverage.cov

生成html报告文件

go tool cover -html coverage.cov -o coverage.html

合并多条覆盖率文件

echo 'mode: count' > ./coverage/system.cov
tail -q -n +2 ./coverage/*.cov >> ./coverage/system.cov

参考文章:
https://www.elastic.co/cn/blog/code-coverage-for-your-golang-system-tests
https://tech.youzan.com/you-zan-go-xiang-mu-dan-ce-ji-cheng-zeng-liang-fu-gai-lu-tong-ji-yu-fen-xi
https://colobu.com/2015/10/09/Linux-Signals

你可能感兴趣的:(Golang 全量覆盖率统计)