golang 统计系统测试覆盖率
2018-03-15 10:27
169 查看
golang 统计系统测试覆盖率
参考资料
https://www.elastic.co/blog/code-coverage-for-your-golang-system-testshttps://www.cnblogs.com/zhaoxd07/p/8028847.html
操作步骤
编写main_test文件
看一下main()函数所在的go文件名称,直接命名为*test.go文件即可。如motan的main()函数在agent.go中,将main test文件命名为agenttest.gomaintest.go与main.go同级目录,如agent.go与agenttest.go同级目录
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() } }
* main_test.go文件是必须存在的,否则无法生成二进制文件
编译代码
将main函数中的os.Exit()更改为return;如果main()函数是启动server,可以再启动一个线程启动server,避免执行到main函数时程序hang住不能执行后面的testcase,如agent.gofunc main() { ext := wmotan.GetWeiboExtentionFactory() wmotan.UpdateLocalIPByConn("") agent := motan.NewAgent(ext) agent.RegisterManageHandler("/200", wmotan.CreateStatusChangeHandler(agent)) agent.RegisterManageHandler("/503", wmotan.CreateStatusChangeHandler(agent)) (&mtool.DebugHandler{}).Register() //另启动一个线程启动服务 go agent.StartMotanAgent() time.Sleep(time.Second * 60) return }
将main.go、maintest.go及main.go中需要的配置文件拷贝到自动化测试工程,如将agent.go及agenttest.go拷贝到测试代码目录下,目录如下:
├── agent.go ├── agent_test.go ├── base │ ├── base.go │ └── config.go ├── clientdemo.yaml ├── manage_test.go ├── motan-go-regression.test ├── motan.yaml ├── protocol_test.go └── server │ ├── serverdemo.go │ └── serverdemo.yaml
编译,参数说明
-c表示生成测试二进制文件
-covermode=count表示生成的二进制中国年包含覆盖率计数信息
-coverpkg后面要统计覆盖率的文件源码
-o 后面是输出的二进制文件名
go test -c -covermode=count -coverpkg .,../../github.com/weibocom/motan-go/,../../github.com/weibocom/motan-go/cluster,../../github.com/weibocom/motan-go/config,../../github.com/weibocom/motan-go/core,../../github.com/weibocom/motan-go/endpoint,../../github.com/weibocom/motan-go/filter,../../github.com/weibocom/motan-go/ha,../../github.com/weibocom/motan-go/lb,../../github.com/weibocom/motan-go/protocol,../../github.com/weibocom/motan-go/provider,../../github.com/weibocom/motan-go/registry,../../github.com/weibocom/motan-go/serialize,../../github.com/weibocom/motan-go/server -o main.test
执行命令,生成一个二进制文件main.test
启动服务: -systemTest用于启动main_test.go中的main函数(启动server);-test.coverprofile用来指定覆盖率信息写入哪个文件
启动服务后也会自动执行当前目录下的测试case
./main.test -systemTest -test.coverprofile coverage.cov
执行完毕后(测试用例执行完毕),在当前目录下会生成coverage.cov文件
统计覆盖率
使用go工具链生成html文件go tool cover -html=./coverage.cov -o coverage.html
在浏览器下打开html文件,格式如下
相关文章推荐
- Xcode5 统计单元测试覆盖率(仅限XCTest)
- Xcode5 统计单元测试覆盖率(仅限XCTest)
- Xcode5 统计单元测试覆盖率(仅限XCTest)
- iOS 手工测试覆盖率率统计实践
- 用c#开发微信 (14) 微统计 - 阅读分享统计系统 4 部署测试 (最终效果图)
- 移动测试基础 jacoco 统计 Android 手工测试覆盖率并自动上报
- shell脚本自动测试+自动覆盖率统计+自动转PDF+自动发邮件
- Xcode5 统计单元测试覆盖率(仅限XCTest)
- 使用Cobertura统计JUnit测试覆盖率
- 测试工程如何引入War包 并使用Clover统计覆盖率
- 关于某些系统统计查询等业务进行大数据量的测试
- Cobertura 统计多模块maven项目测试覆盖率
- c++测试覆盖率统计 —lcov
- 迅雷统计系统开始测试
- 使用EclEmma统计代码的测试覆盖率 [ 光影人像 东海陈光剑 的博客 ]
- 软件项目管理系统-项目管理-模块统计-测试统计
- jacoco统计自动化测试代码覆盖率
- Cobertura 统计多模块maven项目测试覆盖率
- 如何统计测试代码的覆盖率