英文:
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=./...
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论