去测试coverprofile找不到包

时间:2016-09-28 20:19:56

标签: go

当我尝试以下

go test -coverprofile=coverage.out

我得到了这个coverage.out:

mode: set
_/Users/gert/Desktop/httx/auth.go:10.66,11.54 1 0
_/Users/gert/Desktop/httx/auth.go:11.54,13.89 2 0
_/Users/gert/Desktop/httx/auth.go:17.3,17.11 1 0
_/Users/gert/Desktop/httx/auth.go:13.89,16.4 2 0
_/Users/gert/Desktop/httx/auth.go:22.42,25.2 2 0
...

但是当我做的时候

go tool cover -func=coverage.out

coverage.out似乎没有正确格式化?

cover: can't find "auth.go": cannot find package "_/Users/gert/Desktop/httx/" in any of:                                     
        /usr/local/Cellar/go/1.7.1/libexec/src/_/Users/gert/Desktop/httx (from $GOROOT)                                      
        /Users/gert/go/src/_/Users/gert/Desktop/httx (from $GOPATH)

编辑:请注意go test -cover有效。

PASS                                                                                                                         
coverage: 29.7% of statements                                                                                                
ok      _/Users/gert/Desktop/httx       0.015s

2 个答案:

答案 0 :(得分:6)

您的评论包/Users/gert/Desktop/httx/超出$GOPATH /Users/gert/go。将httx包移到$GOPATH下的某个位置,它会正常工作。您可以将其移至/Users/gert/go/src/httx或类似/Users/gert/go/src/github.com/your-github-name/httx(假设您使用GitHub)。

答案 1 :(得分:2)

请参阅use relative path

将覆盖范围路径更改为./

sed -i "s/_$(pwd|sed 's/\//\\\//g')/./g" coverage.out