golang – 代码覆盖率始终显示为 0.0% 的语句覆盖率。

huangapple go评论74阅读模式
英文:

golang - codecoverage always shows coverage: 0.0% of statements

问题

我创建了一个示例的Go项目,并为其创建了单元测试用例(在Linux环境下,go1.3版本)。

当我运行go test时,输出结果如下:

> PASS
ok supported_db 0.201s

我尝试使用go test -cover命令对整个应用程序进行代码覆盖率测试,结果显示:

> go tool: 找不到工具"cover";要安装,请执行以下命令:
go get code.google.com/p/go.tools/cmd/cover

我还尝试在运行特定测试用例时检查覆盖率,使用命令go test -cover CouchDB_test.go,结果显示:

> ok command-line-arguments 0.158s coverage: 0.0% of statements

请帮助我在Go语言中运行代码覆盖率测试。

英文:

I created one sample go project and created a unit test cases for the same (In Linux environment, go1.3 version)

When i ran go test the output would be

> PASS
ok supported_db 0.201s

And i tried to perform code coverage for whole application by using go test -cover command it shows

> go tool: no such tool "cover"; to install:
go get code.google.com/p/go.tools/cmd/cover

Also i checked the coverage while running a specific test case by running go test -cover CouchDB_test.go command it shows

> ok command-line-arguments 0.158s coverage: 0.0% of statements

please help me to run code coverage in golang.

答案1

得分: 37

这是要翻译的内容:

它对我有帮助

如果测试文件在子文件夹中,只需添加-coverpkg=./...选项:

go test ./... -v -coverpkg=./...
英文:

It helped me

Just add -coverpkg=./... option if tests are in sub folders:

go test ./... -v -coverpkg=./...

答案2

得分: 16

首先尝试运行以下命令:

go test -coverprofile=coverage.out

然后运行以下命令查看结果:

go tool cover -html=coverage.out

如果1.3版本是通过升级1.1、1.2等版本安装的,你可以尝试像issue 110中所述的那样进行操作:

> 我通过完全删除 $GOPATH/src/code.google.com/p/go.tools 并重新安装 cover 来解决了这个问题:

go get golang.org/x/tools/cmd/cover
英文:

Try first:

go test -coverprofile=coverage.out

I then run, to see the result:

go tool cover -html=coverage.out

If the 1.3 version was installed through an upgrade of 1.1, 1.2, ..., you can try, as in issue 110:

> I solved this by completely removing $GOPATH/src/code.google.com/p/go.tools and install cover again:

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

huangapple
  • 本文由 发表于 2014年9月1日 19:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25604900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定