英文:
How to run go test on all test files in my project except for vendor packages
问题
我的项目文件夹包含:
Makefile  README.md  component/  driver/  service/  vendor/  worker/
我想在所有测试文件上运行go test,例如foobar_test.go文件,但不包括vendor包中的测试文件。我最接近成功的方法是使用go test ./...,但这会包括vendor测试文件。
我在文档中看到可以在-run选项中传递一个正则表达式,但我在使用时遇到了问题。例如,我尝试了go test ./*,但出现了一堆无法加载包的错误。
有什么最好的方法来做到这一点?
英文:
My project folder contains:
Makefile  README.md  component/  driver/  service/  vendor/  worker/
I'd like to run go test on all test files, e.g. foobar_test.go files except for the test files in the vendor package. The closest I've come to success was with go test ./... but that included vendor test files.
I saw in the documentation you can pass a regex to -run option but I'm having trouble getting this working. For example I tried go test ./*, but I get a bunch of can't load package errors.
What's the best way to do this?
答案1
得分: 42
-run模式仅匹配测试标识符(而不是文件名);原则上,你可以这样做:
go test -run TestFoo
但是这样你将不得不在所有的测试函数名中添加Foo,这可能不是你想要的。
...通配符自Go 1.9版本开始排除了./vendor目录,所以现在你可以直接运行go test ./...,它将不包括./vendor目录。
英文:
The -run pattern is matched only against the test identifier (not the filename); in principle you could do:
go test -run TestFoo
but when you'd have to add Foo to all your test function names, which you probably don't want.
The ... wildcard excludes the ./vendor directory since Go 1.9, so you can now just run go test ./... and it won't include ./vendor.
答案2
得分: 6
cmd/go: 排除供应商包从...匹配
通过压倒性的普遍需求,通过使...从供应商包上面的“vendor”元素永远不匹配,排除供应商包从...匹配。
现在,go help packages 读取:
如果导入路径包含一个或多个“...”通配符,则导入路径是一个模式,每个通配符都可以匹配任何字符串,包括空字符串和包含斜杠的字符串。这样的模式会扩展为在GOPATH树中找到的所有包目录,这些目录的名称与模式匹配。
为了使常见的模式更方便,有两种特殊情况。首先,模式末尾的/...可以匹配空字符串,因此net/...可以同时匹配net和其子目录中的包,如net/http。其次,包含通配符的任何斜杠分隔的模式元素都不参与对供应商包路径中“vendor”元素的匹配,因此./...不会匹配./vendor或./mycode/vendor的子目录中的包,但./vendor/...和./mycode/vendor/...会匹配。但是,请注意,一个包含代码的名为vendor的目录本身不是供应商包:cmd/vendor将是一个名为vendor的命令,模式cmd/...与其匹配。
修复#19090。
go / go / fa1d54c2edad607866445577fe4949fbe55166e1
提交fa1d54c2edad607866445577fe4949fbe55166e1
2017年3月29日18:51:44 +0000
尝试在tip上运行go test ./...或等待Go1.9。
英文:
> cmd/go: exclude vendor dir from matching ... #19090
>
>  cmd/go: exclude vendored packages from ... matches
>
>     By overwhelming popular demand, exclude vendored packages from ... matches,
>     by making ... never match the "vendor" element above a vendored package.
>  
>     go help packages now reads:
>  
>         An import path is a pattern if it includes one or more "..." wildcards,
>         each of which can match any string, including the empty string and
>         strings containing slashes.  Such a pattern expands to all package
>         directories found in the GOPATH trees with names matching the
>         patterns.
>  
>         To make common patterns more convenient, there are two special cases.
>         First, /... at the end of the pattern can match an empty string,
>         so that net/... matches both net and packages in its subdirectories, like net/http.
>         Second, any slash-separted pattern element containing a wildcard never
>         participates in a match of the "vendor" element in the path of a vendored
>         package, so that ./... does not match packages in subdirectories of
>         ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do.
>         Note, however, that a directory named vendor that itself contains code
>         is not a vendored package: cmd/vendor would be a command named vendor,
>         and the pattern cmd/... matches it.
>  
>     Fixes #19090.
>
> go / go / fa1d54c2edad607866445577fe4949fbe55166e1
>
>     commit	fa1d54c2edad607866445577fe4949fbe55166e1
>     Wed Mar 29 18:51:44 2017 +0000
Try running go test ./... at tip or wait for Go1.9.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论