登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-02-07 18:36:22 178浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《如何仅编译存储库中的所有测试而不运行它们》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

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

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

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

我看到 -c 标志:

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

但是...不能将 -c 标志与 -build 标志一起使用:

$ go test -build=integration -c ./integrationtests/client/admin-api go: 未知标志 -build=integration 不能与 -c 一起使用

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

$ 去测试 -c ./... 不能对多个包使用 -c 标志

有什么想法吗?


正确答案


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

go test -run=xxx_should_never_match_xxx ./...

ok      mypkg       0.731s [no tests to run]

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

如果您需要传递通常在 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 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.

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>