在另一个包中测试时获取Coverage统计信息 [英] Get Coverage stats when tests are in another package
问题描述
我的测试与我的代码不在同一个包中。我发现这是一个用很多测试文件组织代码库的混乱方式,我已经读过,为了限制测试通过包的公共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覆盖率:语句的100.0%
。但实际上这只是覆盖我的 UtilityFunction.go
(正如我说的当我运行去测试bandsintown-api / api_client_tests -cover = cover.out
和
go tool cover -html = cover.out
)。
是否有任何方法可以获得被测试的实际 api_client
包的覆盖范围,而无需将它全部放入同一个包中?
正如评论中提到的那样,您可以运行 go test -cover -coverpkgapi_clientapi_client_tests
运行覆盖率测试。
但是将代码文件从测试文件分解到不同的目录不是Go的方式。
我想你想要一个黑盒子测试(即使是测试,任何软件包私有内容都不能在外部访问)。为了达到这个目的,它允许在另一个软件包中进行测试(不移动文件)。例子:
api_client.go
package api_client
//在包之外不可访问
var privateVar = 10
func方法(){
}
api_client_test.go
package api_client_tests
testtesting
func TestClient(t * testing.T){
Method()
}
My tests aren't in the same package as my code. I find this a less cluttered way of organising a codebase with a lot of test files, and I've read that it's a good idea in order to limit tests to interacting via the package's public api.
So it looks something like this:
api_client:
Client.go
ArtistService.go
...
api_client_tests
ArtistService.Events_test.go
ArtistService.Info_test.go
UtilityFunction.go
...
I can type go test bandsintown-api/api_client_tests -cover
and see 0.181s coverage: 100.0% of statements
. But that's actually just coverage over my UtilityFunction.go
(as I say when I ran go test bandsintown-api/api_client_tests -cover=cover.out
and
go tool cover -html=cover.out
).
Is there any way to get the coverage for the actual api_client
package under test, without bringing it all into the same package?
As it is mentioned in comments you can run
go test -cover -coverpkg "api_client" "api_client_tests"
to run the tests with coverage.
But splitting code files from tests files to a different directories isn't a Go's way.
I suppose that you want to have a black-box testing(nothing package-private stuff can be accessible outside, even for tests).
To accomplish this it's allowed to have tests in another package(without moving the files). Example:
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()
}
这篇关于在另一个包中测试时获取Coverage统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!