golang代码覆盖率

栏目: Go · 发布时间: 5年前

内容简介:参考文章:注意:1 命名:看一下自己代码的main()函数所在的go文件名称,直接命名为*_test.go文件即可; 比如,代码文件名 main_server.go,可直接命名为main_server_test.go

参考文章: <u>https://studygolang.com/articles/11934</u>

一 Main函数的test文件

package main

// This file is mandatory as otherwise the filebeat.test binary is not generated correctly.

import (

  "testing"

  "flag"

)

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()

  }

}

注意:

1 命名:看一下自己代码的main()函数所在的 go 文件名称,直接命名为*_test.go文件即可; 比如,代码文件名 main_server.go,可直接命名为main_server_test.go

2 目录:放在与被测文件同目录下

二 编译代码

1 查看main函数,将其中的os.Exit()更改为return (根据实际情况分别为return/return 0/return 255...)

2 原编译命令为go build

此处使用命令go test -c -covermode=count -coverpkg ./gopath/src/mvdsp/module/,./gopath/src/mvdsp/extractor/,./gopath/src/mvdsp/mvutil/,./gopath/src/mvdsp/protocol/ -o dsp_server.test

① -c 表示 生成测试二进制文件

② -covermode=count 表示 生成的二进制中包含覆盖率计数信息

③ -coverpkg 后面是要统计覆盖率的文件源码目录

④ -o 后面是输出的二进制文件名(默认为 packetname.test)

3 执行命令,生成一个可执行的二进制文件,拷贝到部署目录下

4 启动服务,在启动命令后加参数: -systemTest -test.coverprofile coverage/coverage.cov

① -systemTest 用来启动前面说过的main test

② -test.coverprofile 用来指定覆盖率信息写入到哪个文件

三 统计覆盖率

1 执行自动化

2 执行如下命令,生成覆盖率文件coverage.cov

killall -2 packagename.test

3 生成测试报告

go tool cover -html=./coverage/coverage.cov -o /data/reports/coverage.html


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

TensorFlow实战

TensorFlow实战

黄文坚、唐源 / 电子工业出版社 / 2017-2-1 / 79

Google近日发布了TensorFlow 1.0候选版,这个稳定版将是深度学习框架发展中的里程碑的一步。自TensorFlow于2015年底正式开源,距今已有一年多,这期间TensorFlow不断给人以惊喜,推出了分布式版本,服务框架TensorFlow Serving,可视化工具TensorFlow,上层封装TF.Learn,其他语言(Go、Java、Rust、Haskell)的绑定、Wind......一起来看看 《TensorFlow实战》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具