简体   繁体   English

如何忽略 Go 测试覆盖率中生成的文件

[英]How to ignore generated files from Go test coverage

I have a generated file in my package with DO NOT EDIT on top.我的包中有一个生成的文件,上面有DO NOT EDIT I am running tests for my package with go test -coverprofile=cover.out <package> .我正在使用go test -coverprofile=cover.out <package>为我的包运行测试。 This creates coverage profile and shows total coverage percentage.这将创建覆盖率配置文件并显示总覆盖率百分比。 But it also includes generated files while calculating the coverage.但它也包括在计算覆盖率时生成的文件。 Is there a way to ignore generated files in coverage calculation?有没有办法在覆盖率计算中忽略生成的文件?

You could strip the generated code from the cover profiles:您可以从封面配置文件中删除生成的代码:

go test . -coverprofile cover.out.tmp
cat cover.out.tmp | grep -v "_generated.go" > cover.out
tool cover -func cover.out

Depending on the used tools this can be implemented easily in the pipeline/make.根据使用的工具,这可以在管道/制作中轻松实现。

Most Go tools operate on packages, because a package itself forms a unit that may be useful in its entirety.大多数 Go 工具对包进行操作,因为包本身形成了一个整体可能有用的单元 Excluding files from a package could easily "break" the package: the excluded file may contain (crucial) package initialization code or may even cause the remaining package files fail to compile.从包中排除文件很容易“破坏”包:被排除的文件可能包含(关键)包初始化代码,甚至可能导致剩余的包文件无法编译。

go test is no exception: it also operates on packages. go test也不例外:它也对包进行操作。 There is no first-hand support to exclude files from a package.没有第一手支持从包中排除文件。

If your package may be compiled and tested without the generated file, you may opt to generate the file into a different package, and then it won't be included in your package's (coverage) test naturally.如果您的包可能在没有生成文件的情况下被编译和测试,您可以选择将文件生成到不同的包中,然后它自然不会包含在您的包的(覆盖率)测试中。

Another way to handle this would be to still generate it in the same package / folder, and use special build tags in the generated files, which you may exclude when running the coverage test.处理此问题的另一种方法是仍然在同一个包/文件夹中生成它,并在生成的文件中使用特殊的构建标记,您可以在运行覆盖测试时排除这些标记。 You can read more about build tags here: Build Constraints , and here: What is the right approach to encapsulate platform specific code in Go?您可以在此处阅读有关构建标记的更多信息: Build Constraints和此处: 在 Go 中封装特定于平台的代码的正确方法是什么?

If the generated file is needed to compile / test the package, then you still have an option: use an internal package for the generated file.如果需要生成的文件来编译/测试包,那么您仍然有一个选择:对生成的文件使用内部包。 Internal packages are only available to the package tree rooted at the internal folder, which means you may export any identifiers in an internal package, the compiler ensures they won't be used by "unintended" parties.内部包仅可用于以internal文件夹为根的包树,这意味着您可以导出内部包中的任何标识符,编译器确保它们不会被“非预期”方使用。 You can read more about internal packages here: Can I develop a go package in multiple source directories?您可以在此处阅读有关内部包的更多信息: 我可以在多个源目录中开发一个 go 包吗?

Also consider the option to write or generate test for the generated code, which may be good practice anyway, so you wouldn't have to use tricks to exclude them from coverage tests.还要考虑为生成的代码编写或生成测试的选项,无论如何这可能是一种很好的做法,因此您不必使用技巧将它们从覆盖测试中排除。

Following the ways that I could solve this need.按照我可以解决这个需求的方法。

By excluding dirs/pkgs通过排除目录/包

Since to run the test and generated the cover you will specify the pkgs where the go test command should look for the files out of this pkg will be ignored automatically.由于要运行测试并生成封面,您将指定 go test 命令应该从该 pkg 中查找文件的 pkgs 将被自动忽略。 Following an example下面是一个例子

project
|_______/pkg/web/app 
|_______/pkg/web/mock -> It will be ignored.

# Command:   
$go test -failfast -tags=integration -coverprofile=coverage.out -covermode=count github.com/aerogear/mobile-security-service/pkg/web/apps

However, it cannot be applied in all cases.但是,它不能适用于所有情况。 For example, in the case of the files be mock routines of your interfaces it may not work very well because the imports which are required into the service, test and mock probably will be in a cycle and in this not allowed.例如,在文件是接口的模拟例程的情况下,它可能无法很好地工作,因为服务、测试和模拟所需的导入可能会处于循环中,并且在这种情况下是不允许的。

By using "_test" in the name definitions通过在名称定义中使用“_test”

If you would like to ignore files which are used in the tests then make sense use _test at the end of the name.如果您想忽略测试中使用的文件,那么在名称末尾使用_test是有意义的。 For example, my_service_mock_test.go .例如, my_service_mock_test.go The files with _test at the end of the name will be ignored by default.默认情况下,名称末尾带有_test的文件将被忽略。

By removing from the cover file as follows通过从封面文件中删除如下

go test . -coverprofile cover.out.tmp
cat cover.out.tmp | grep -v "_generated.go" > cover.out
tool cover -func cover.out

PS.: I could not find a comment or tag which would exclude them. PS.:我找不到可以排除它们的评论或标签。

You can grep those directories out from the test.您可以从测试中提取出这些目录。

So, instead of:所以,而不是:

go test ./...

You do this:你做这个:

go test `go list ./... | grep -v ./excluded/autogen/directory/here`

Also, make sure that you don't add a trailing / .另外,请确保不要添加尾随/ It doesn't work perfectly.它不能完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM