[英]Golang: Compile all tests across a repo without executing them
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.所有这些测试都隐藏在
//go:build integration
标志后面,所以如果我go
查看或运行它们,我需要将-build integration
标志传递给测试命令。
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.我想要完成的是在整个 repo 中编译所有测试而不实际执行它们(需要很长时间),所以我可以捕获 PR 引入的构建错误,而默认的
go test
编译和执行不会捕获。
I see the -c
flag:我看到
-c
标志:
-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).
-c 将测试二进制文件编译为 pkg.test 但不运行它(其中 pkg 是包导入路径的最后一个元素)。 The file name can be changed with the -o flag.
可以使用 -o 标志更改文件名。
However… one cannot use the -c
flag with the -build
flag:但是......不能将
-c
标志与-build
标志一起使用:
$ go test -build=integration -c ./integrationtests/client/admin-api
$ go test -build=integration -c ./integrationtests/client/admin-api
go: unknown flag -build=integration cannot be used with -c
go: 未知标志 -build=integration 不能与 -c 一起使用
Also... one cannot use the -c
flag across multiple packages:另外......不能在多个包中使用
-c
标志:
$ go test -c ./...
$ 去测试 -c ./...
cannot use -c flag with multiple packages
不能对多个包使用 -c 标志
Any ideas?有任何想法吗?
You can use the go test -run
flag and pass it a pattern you know will never match:你可以使用
go test -run
标志并传递一个你知道永远不会匹配的模式:
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 (eg go build -tags mytag
), you can do the same during go test
:如果您需要传递通常在
go build
过程中传递的任何构建标签(例如go build -tags mytag
),您可以在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:内联 (
go help testflag
) 文档中有关-run
标志的完整详细信息:
-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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.