如何编译存储库中的所有测试而不执行它们

Bre*_*ton 4 unit-testing go go-build

我有一个存储库,其中多个团队贡献集成测试。

\n

所有这些测试都隐藏在//go:build integration标志后面,因此如果我想go查看或运行它们,我需要将标志传递-build integration给测试命令。

\n

我\xe2\x80\x99m 试图完成的是编译整个存储库中的所有测试,而不实际执行它们(需要很长时间),这样我就可以捕获 PR 引入的构建错误,而默认编译go test和执行不会抓住。

\n

我看到-c旗帜:

\n
\n

-c\n将测试二进制文件编译为 pkg.test 但不运行它\n(其中 pkg 是包导入路径的最后一个元素)。\n可以使用 -o 标志更改文件名。

\n
\n

然而\xe2\x80\xa6 不能将该-c标志与该-build标志一起使用:

\n
\n

$ go test -build=integration -c ./integrationtests/client/admin-api

\n

go: 未知标志 -build=integration 不能与 -c 一起使用

\n
\n

另外...不能-c在多个包中使用该标志:

\n
\n

$ 去测试 -c ./...

\n

不能对多个包使用 -c 标志

\n
\n

有任何想法吗?

\n

col*_*tor 6

您可以使用该go test -run标志并向其传递一个您知道永远不会匹配的模式:

go test -run=XXX_SHOULD_NEVER_MATCH_XXX ./...

ok      mypkg       0.731s [no tests to run]
Run Code Online (Sandbox Code Playgroud)

这将捕获任何测试编译错误 - 如果没有 - 则不会运行任何测试。

如果您需要传递通常在go build流程中传递的任何构建标签(例如go build -tags mytag),您可以在 期间执行相同操作go test

go test -tags mytag -run=XXX_SHOULD_NEVER_MATCH_XXX ./...
Run Code Online (Sandbox Code Playgroud)

-run内联 ( ) 文档中有关该标志的完整详细信息go help testflag

        -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.
Run Code Online (Sandbox Code Playgroud)