如何检测 GO 中分离文件夹的代码覆盖率?
How to detect code-coverage of separated folders in GO?
我的项目结构
stuff/stuff.go -> package: stuff
test/stuff/stuff_test.go -> package: test
尽管 stuff_test 从 stuff.go 执行代码,但它显示
coverage: 0.0% of statements
我用了
go test -cover
如果我将我的 *_test.go 移动到程序的 stuff-folder 它工作正常。
或许我的项目结构方法不对designed/go-conform?
常规的 Go 程序结构将测试与包放在一起。像这样:
project
|-stuff
|--stuff.go
|--stuff_test.go
在您的测试文件的顶部,您仍然声明 package stuff
,并且如果您希望 go test
自动 [=21],则要求您的测试方法采用 TestMethodX
形式=]他们。
有关详细信息,请参阅 Go 文档:https://golang.org/pkg/testing/
Cross-package 不直接支持测试覆盖率,但有几个人构建了包装器来合并各个覆盖率配置文件。
参见Issue #6909 for the long history on this. And see gotestcover for an example tool to do the merging. There's also gocovmerge。我构建了自己的版本,所以我没有尝试过任何这些,但我确信它们都像我的一样工作,而且我的工作正常。
我的感觉是,这只是一个问题,没有人为此编写真正引人注目的更改列表,并且对核心维护者来说并不那么重要,因此尚未解决。它确实提出了可能会破坏现有测试的小角落案例,因此对我们大多数人有用的快速黑客还没有被接受 as-is。但是我没有看到任何讨论表明核心维护者积极反对此功能。
您可以使用 -coverpkg
选项 select 记录覆盖率信息的包。
来自go help testflag
的输出:
-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.
例如:
go test ./test/... -coverprofile=cover.out -coverpkg ./...
然后查看报告:
go tool cover -html=cover.out
我的项目结构
stuff/stuff.go -> package: stuff
test/stuff/stuff_test.go -> package: test
尽管 stuff_test 从 stuff.go 执行代码,但它显示
coverage: 0.0% of statements
我用了
go test -cover
如果我将我的 *_test.go 移动到程序的 stuff-folder 它工作正常。
或许我的项目结构方法不对designed/go-conform?
常规的 Go 程序结构将测试与包放在一起。像这样:
project
|-stuff
|--stuff.go
|--stuff_test.go
在您的测试文件的顶部,您仍然声明 package stuff
,并且如果您希望 go test
自动 [=21],则要求您的测试方法采用 TestMethodX
形式=]他们。
有关详细信息,请参阅 Go 文档:https://golang.org/pkg/testing/
Cross-package 不直接支持测试覆盖率,但有几个人构建了包装器来合并各个覆盖率配置文件。
参见Issue #6909 for the long history on this. And see gotestcover for an example tool to do the merging. There's also gocovmerge。我构建了自己的版本,所以我没有尝试过任何这些,但我确信它们都像我的一样工作,而且我的工作正常。
我的感觉是,这只是一个问题,没有人为此编写真正引人注目的更改列表,并且对核心维护者来说并不那么重要,因此尚未解决。它确实提出了可能会破坏现有测试的小角落案例,因此对我们大多数人有用的快速黑客还没有被接受 as-is。但是我没有看到任何讨论表明核心维护者积极反对此功能。
您可以使用 -coverpkg
选项 select 记录覆盖率信息的包。
来自go help testflag
的输出:
-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.
例如:
go test ./test/... -coverprofile=cover.out -coverpkg ./...
然后查看报告:
go tool cover -html=cover.out