-封面配置文件从覆盖百分比中排除未经测试的文件



我正在尝试对包含多个目录的项目运行测试覆盖率报告(我知道这是一个不寻常的设置,但它有很多我想组合在一起的示例(:

└── example
├── bar
│   └── main.go
├── baz
│   └── main.go
├── foo
│   ├── main.go
│   └── main_test.go
└── qux
└── main.go

但是当它运行时,我得到一些奇怪的输出。覆盖率似乎只与唯一测试的文件有关 - 而不是example下的整个代码库。

例如:

$ go test -race -v -coverprofile .test-coverage.txt ./...
?       github.com/abcdef/example/bar    [no test files]
?       github.com/abcdef/example/baz      [no test files]
=== RUN   TestSum
--- PASS: TestSum (0.00s)
PASS
coverage: 50.0% of statements
ok      github.com/abcdef/example/foo     1.018s  coverage: 50.0% of statements
?       github.com/abcdef/example/qux    [no test files]

有谁知道为什么会发生这种情况,以及是否有办法让它在所有文件(包括任何未经测试的文件(中生成百分比覆盖率?否则,覆盖率完全具有误导性。

使用-coverpkg标志,描述如下:

将每个测试中的覆盖率分析应用于与模式匹配的包。默认设置是每个测试仅分析正在测试的包。有关包模式的说明,请参阅"转到帮助包"。设置-封面。

例:

go test -race -v -coverprofile .test-coverage.txt -coverpkg=example ./...

这告诉去在覆盖范围统计信息中包含包example。但是,请务必使用软件包名称的完整版本。 即github.com/user/example

最新更新