简体   繁体   中英

go test coverprofile cannot find package

When I try the following

go test -coverprofile=coverage.out

I get this 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
...

But when I then do

go tool cover -func=coverage.out

coverage.out doesn't seem to be correctly formatted?

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)

EDIT: Note that go test -cover works.

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

Your package /Users/gert/Desktop/httx/ is outside of your $GOPATH which is /Users/gert/go . Move the httx package somewhere under your $GOPATH and it will work fine. You could move it to /Users/gert/go/src/httx or probably something like /Users/gert/go/src/github.com/your-github-name/httx (assuming you use GitHub).

see use relative path

change coverage path to ./ :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM