去测试 coverprofile 找不到包

标签 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

最佳答案

您的包 /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)。

关于去测试 coverprofile 找不到包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756930/

相关文章:

json - 将任意字段添加到未知结构的 json 输出

string - ResponseWriter.Write 和 io.WriteString 有什么区别?

go - 尝试在Go中设置Cookie,但收到一条CORS错误,指出Access-Control-Allow-Credentials未设置为true

json - 无法在 google go 中遍历已解析的 JSON

go - 关于 $GOPATH 的一些问题

go - 一个基本的 Golang 流( channel )死锁

python - 在Windows操作系统上使用Go和Python实现客户端-服务器模型

go - Golang 中处理多个错误的最佳实践

go - "go.etcd.io/etcd/clientv3/balancer/picker"- 未定义 : balancer. PickOptions

go - 如何添加新路由来自动生成资源?