Go Coverage不包括其他软件包中的功能 [英] Go Coverage not including functions in other package

查看:44
本文介绍了Go Coverage不包括其他软件包中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的项目:

I have a project like this:

app/
    api/
        foo.go
        test_foo.go
src/
    db/
        bar.go

foo.go 中,对 bar.go 中的函数进行了调用.但是,运行覆盖率报告时,它显示bar.go的0行被覆盖了.

In foo.go, a call is made to a function in bar.go. However, when running the coverage report, it shows that 0 lines of bar.go are covered.

有没有一种方法可以覆盖其他程序包的函数调用?

Is there a way to get coverage to include function call to other packages?

就我而言,我不想对 db api 进行单独的测试,因为通过 db 进行的所有调用将始终进行通过 api ,编写两个测试将是多余的.

In my case, I do not want to make separate tests for db and for api, since all calls through db will always go through api, and it would be redundant to write two tests.

我正在像这样运行覆盖范围:

I'm running the coverage like this:

go clean -testcache
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

推荐答案

您需要添加 coverpkg 标志,以便:

You need to include the coverpkg flag so:

 go test -coverpkg=./... coverprofile=coverage.out ./...

将计算所有包裹的覆盖范围.

Would calculate the coverage on all of your packages.

https://golang.org/cmd/go/#hdr-Testing_flags

这篇关于Go Coverage不包括其他软件包中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆