猿问

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

我正在尝试在我的项目中的所有包中获得测试覆盖率。


测试成功执行,并在执行以下命令时报告覆盖范围。


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 directory

testing: warning: no tests to run

PASS

coverage: 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


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


炎炎设计
浏览 228回答 2
2回答

肥皂起泡泡

有没有办法找到构建失败的原因?单独生成(或测试)包。

慕运维8079593

我没有直接的答案,但我遇到了类似的问题,也许我的过程可以帮助您解决问题。首先,是你的朋友。Go 文档、参考和 Go 一般都是关于提高你的工作效率。因此,Go团队投入了大量精力来解释好事情。go helpgo 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标志解析有关。请参阅在测试文件中使用主销)
随时随地看视频慕课网APP

相关分类

Go
我要回答