简体   繁体   English

Golang:在一个 repo 中编译所有测试而不执行它们

[英]Golang: Compile all tests across a repo without executing them

Context语境

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标志传递给测试命令。

Purpose目的

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.

相关问题 karma jasmine 没有执行所有测试 - karma jasmine not executing all tests sbt test:compile 不编译所有测试 - sbt test:compile does not compile all tests 在一个编译单元中运行所有 catch2 测试,无需标签定义 - Run all catch2 tests in one compile unit without tag definition 您是否可以从命令行在包中运行所有JUnit测试而无需明确列出它们? - Can you run all JUnit tests in a package from the command line without explicitly listing them? 如何使用visual studio 2012执行系统或集成测试,而不将它们包括在运行所有测试中? - How can I perform system or integration tests using visual studio 2012 without including them in run all tests? 仅使用pytest执行测试而不执行父脚本 - Executing only tests using pytest without executing parent script 创建nunit测试而不用api导出它们 - Creating nunit tests without exporting them with the api 运行所有测试,但停止并调试其中一个 - Running all tests but stop and debug one of them Junit:测试选定的测试,而不是全部测试 - Junit: testing chosen tests instead of all of them 如何运行所有 Golang 基准测试,包括子文件夹 - How to run all Golang benchmark tests including subfolders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM