当测试在另一个包中时获取覆盖率统计信息
Get Coverage stats when tests are in another package
我的测试与我的代码不在同一个包中。我发现这是一种使用大量测试文件组织代码库的不那么混乱的方法,而且我读到这是一个好主意,可以将测试限制为通过包的 public api 进行交互。
所以它看起来像这样:
api_client:
Client.go
ArtistService.go
...
api_client_tests
ArtistService.Events_test.go
ArtistService.Info_test.go
UtilityFunction.go
...
我会打字go test bandsintown-api/api_client_tests -cover
并查看 0.181s coverage: 100.0% of statements
。但这实际上只是对我的 UtilityFunction.go
的报道(正如我在 运行 go test bandsintown-api/api_client_tests -cover=cover.out
和
go tool cover -html=cover.out
).
是否有任何方法可以覆盖实际的 api_client
被测包,而无需将其全部放入同一个包中?
如评论中所述,您可以运行
go test -cover -coverpkg "api_client" "api_client_tests"
到 运行 覆盖测试。
但是将代码文件从测试文件拆分到不同的目录并不是 Go 的方式。
我想你想进行黑盒测试(没有包私有的东西可以在外面访问,即使是测试)。
为了完成这个,允许在另一个包中进行测试(不移动文件)。示例:
api_client.go
package api_client
// will not be accessible outside of the package
var privateVar = 10
func Method() {
}
api_client_test.go
package api_client_tests
import "testing"
func TestClient(t *testing.T) {
Method()
}
我的测试与我的代码不在同一个包中。我发现这是一种使用大量测试文件组织代码库的不那么混乱的方法,而且我读到这是一个好主意,可以将测试限制为通过包的 public api 进行交互。
所以它看起来像这样:
api_client:
Client.go
ArtistService.go
...
api_client_tests
ArtistService.Events_test.go
ArtistService.Info_test.go
UtilityFunction.go
...
我会打字go test bandsintown-api/api_client_tests -cover
并查看 0.181s coverage: 100.0% of statements
。但这实际上只是对我的 UtilityFunction.go
的报道(正如我在 运行 go test bandsintown-api/api_client_tests -cover=cover.out
和
go tool cover -html=cover.out
).
是否有任何方法可以覆盖实际的 api_client
被测包,而无需将其全部放入同一个包中?
如评论中所述,您可以运行
go test -cover -coverpkg "api_client" "api_client_tests"
到 运行 覆盖测试。
但是将代码文件从测试文件拆分到不同的目录并不是 Go 的方式。
我想你想进行黑盒测试(没有包私有的东西可以在外面访问,即使是测试)。
为了完成这个,允许在另一个包中进行测试(不移动文件)。示例:
api_client.go
package api_client
// will not be accessible outside of the package
var privateVar = 10
func Method() {
}
api_client_test.go
package api_client_tests
import "testing"
func TestClient(t *testing.T) {
Method()
}