本文介绍了如何测量Golang中的代码覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人成功为Go单元测试生成代码覆盖率?我无法在网上找到该工具。

注意,现在将显示



  $ go get golang.org/x/tools/cmd / cover 





  $ go test -coverprofile fmt 
ok fmt 0.060s覆盖率:报表的91.4%
$





  go test -coverprofile< filename> <包名称> 





  $ go help testflag 
$ go tool cover -help






提及:

我甚至不想等浏览器打开,所以我定义了这个别名:

 别名gc = grep -v -e1 $coverage.out 

我只需输入 gc ,并列出所有行 ( coverage.out 行 not 结尾为< 1 c $ c>)。


Has anyone succeeded in generating code coverage for Go unit tests? I can't find a tool for that on the web.

解决方案

Note that Go 1.2 (Q4 2013, rc1 is available) will now display test coverage results:

$ go get golang.org/x/tools/cmd/cover
$ go test -coverprofile fmt
ok      fmt 0.060s  coverage: 91.4% of statements
$

Frank Shearar mentions:

go test -coverprofile <filename> <package name>
$ go help testflag
$ go tool cover -help


Ivan Black mentions in the comments:

I don't even want to wait for the browser to open, so I defined this alias:

alias gc=grep -v -e " 1$" coverage.out

That I just type gc, and have a list of all the lines not yet covered (here: with a coverage.out line not ending with " 1").

这篇关于如何测量Golang中的代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:46