为了账号安全,请及时绑定邮箱和手机立即绑定

go 测试在提供 -coverpkg 参数时失败

go 测试在提供 -coverpkg 参数时失败

Go
炎炎设计 2022-08-24 18:43:52
我正在尝试在我的项目中的所有包中获得测试覆盖率。测试成功执行,并在执行以下命令时报告覆盖范围。go test -cover ./...但是当我使用参数执行时,所有测试都失败了go testcoverpkg=./...go test -cover -coverpkg=./... ./...这是命令的示例输出srimal@srimal-pc:~/projects/myproject$ go test -v -cover -coverpkg=./... ./...go build a.abc.com/path/to/module/e2e: no non-test Go files in /home/srimal/projects/driver-selection-handler/e2e?       a.abc.com/path/to/module      [no test files]?       a.abc.com/path/to/module/app  [no test files]?       a.abc.com/path/to/module/constrain    [no test files]FAIL    a.abc.com/path/to/module/directionalhire [build failed]?       a.abc.com/path/to/module/domain       [no test files]FAIL    a.abc.com/path/to/module/durationmatrix [build failed]FAIL    a.abc.com/path/to/module/durationmatrix/etaservice [build failed]FAIL    a.abc.com/path/to/module/durationmatrix/roadmatrix [build failed]2021/04/22 17:23:07 go-util/log: Cannot open config file  open config/logger.yaml: no such file or directorytesting: warning: no tests to runPASScoverage: 0.0% of statements in ./...ok      a.abc.com/path/to/module/e2e  (cached)        coverage: 0.0% of statements in ./... [no tests to run]?       a.abc.com/path/to/module/events       [no test files]FAIL    a.abc.com/path/to/module/finance [build failed]?       a.abc.com/path/to/module/internal     [no test files]?       a.abc.com/path/to/module/internal/config      [no test files]?       a.abc.com/path/to/module/internal/logger      [no test files]?       a.abc.com/path/to/module/internal/metrics     [no test files]?       a.abc.com/path/to/module/internal/profiling   [no test files]?       a.abc.com/path/to/module/internal/schema      [no test files]?       a.abc.com/path/to/module/internal/stream      [no test files]我使用的是 go 版本 1.15.6有没有办法找到构建失败的原因?
查看完整描述

2 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

有没有办法找到构建失败的原因?

单独生成(或测试)包。


查看完整回答
反对 回复 2022-08-24
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

我没有直接的答案,但我遇到了类似的问题,也许我的过程可以帮助您解决问题。


首先,是你的朋友。Go 文档、参考和 Go 一般都是关于提高你的工作效率。因此,Go团队投入了大量精力来解释好事情。go help


go help test是开始获取有关标志信息的好地方,但它没有列出 .然而,它确实指出:-coverpkggo help testflag


测试二进制文件还接受控制测试执行的标志;这些标志也可以通过“go test”访问。有关详细信息,请参阅“转到帮助测试标志”。


go help testflag有这样说的:-coverpkg


-coverpkg pattern1,pattern2,pattern3

    Apply coverage analysis in each test to packages matching the patterns.

    The default is for each test to analyze only the package being tested.

    See 'go help packages' for a description of package patterns.

    Sets -cover.

强调“默认是每个测试仅分析正在测试的包”,这意味着如果我依赖于包函数,如果我逐个包地工作,我可能会遇到问题。init()


如果你想遵循单独运行每个包的建议,你可以在模块的根目录中使用获取所有包的列表,然后使用一些shell脚本来循环访问它们。coverpkggo list ./...


最终,在我的情况下,我推断出这是逐个测试每个包,因此可能逐个加载包。因此,我能够将问题范围缩小到包的功能。不知道为什么它会导致问题,但是从函数中移出几行可以进入函数,解决了我的问题。-coverpkginit()init()main()


(我也很幸运,错误消息很容易谷歌,我知道它与CLI标志解析有关。请参阅在测试文件中使用主销)


查看完整回答
反对 回复 2022-08-24
  • 2 回答
  • 0 关注
  • 227 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号