英文:
How to compile all tests across a repo without executing them
问题
我有一个存储库,多个团队贡献了集成测试。
所有这些测试都隐藏在//go:build integration
标志后面,所以如果我想让go
看到或运行它们,我需要将-build integration
标志传递给测试命令。
我想要实现的目标是在整个存储库中编译所有测试,而不实际执行它们(这将花费很长时间),这样我就可以捕捉到默认的go test
编译和执行无法捕捉到的由PR引入的构建错误。
我看到了-c
标志:
> -c
> 编译测试二进制文件为pkg.test,但不运行它
> (其中pkg是包导入路径的最后一个元素)。
> 可以使用-o标志更改文件名。
然而...不能将-c
标志与-build
标志一起使用:
> $ go test -build=integration -c ./integrationtests/client/admin-api
>
> go: 未知标志 -build=integration 不能与 -c 一起使用
而且...不能在多个包上使用-c
标志:
> $ go test -c ./...
>
> 无法在多个包上使用 -c 标志
有什么想法吗?
英文:
I have a repo in which multiple teams contribute integration tests.
All these tests are hidden behind //go:build integration
flags, so if I want go
to see or run them, I need to pass the -build integration
flag to the test command.
What I’m trying to accomplish is to compile all tests across the entirety of this repo without actually executing them (would take a long time) so I can catch build errors introduced by PRs that the default go test
compilation and execution would not catch.
I see the -c
flag:
> -c
> Compile the test binary to pkg.test but do not run it
> (where pkg is the last element of the package's import path).
> The file name can be changed with the -o flag.
However… one cannot use the -c
flag with the -build
flag:
> $ go test -build=integration -c ./integrationtests/client/admin-api
>
> go: unknown flag -build=integration cannot be used with -c
Also... one cannot use the -c
flag across multiple packages:
> $ go test -c ./...
>
> cannot use -c flag with multiple packages
Any ideas?
答案1
得分: 5
你可以使用go test -run
标志,并将其传递给一个你知道永远不会匹配的模式:
go test -run=XXX_SHOULD_NEVER_MATCH_XXX ./...
这将捕获任何测试编译错误 - 如果没有错误,则不会运行任何测试。
如果你需要传递任何通常在go build
过程中传递的构建标签(例如go build -tags mytag
),你可以在go test
期间执行相同的操作:
go test -tags mytag -run=XXX_SHOULD_NEVER_MATCH_XXX ./...
有关-run
标志的完整详细信息,请参阅内联文档(go help testflag
):
-run regexp
仅运行与正则表达式匹配的测试、示例和模糊测试。对于测试,正则表达式通过未加括号的斜杠(/)字符拆分为一系列正则表达式,测试标识符的每个部分必须与序列中的相应元素匹配(如果有)。请注意,还会运行可能的匹配父级,因此`-run=X/Y`会匹配并运行并报告与X匹配的所有测试的结果,即使没有与Y匹配的子测试,因为它必须运行它们以查找这些子测试。
英文:
You can use the go test -run
flag and pass it a pattern you know will never match:
go test -run=XXX_SHOULD_NEVER_MATCH_XXX ./...
ok mypkg 0.731s [no tests to run]
this will catch any test compilation errors - and if there are none - no tests will be run.
If you need to pass any build tags that are typically passed during your go build
process (e.g. go build -tags mytag
), you can do the same during go test
:
go test -tags mytag -run=XXX_SHOULD_NEVER_MATCH_XXX ./...
Full details on the -run
flag from the inline (go help testflag
) docs:
-run regexp
Run only those tests, examples, and fuzz tests matching the regular
expression. For tests, the regular expression is split by unbracketed
slash (/) characters into a sequence of regular expressions, and each
part of a tests identifier must match the corresponding element in
the sequence, if any. Note that possible parents of matches are
run too, so that -run=X/Y matches and runs and reports the result
of all tests matching X, even those without sub-tests matching Y,
because it must run them to look for those sub-tests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论