如何对我的项目中除供应商包之外的所有测试文件运行测试

时间:2017-04-19 23:29:58

标签: testing go packages

我的项目文件夹包含:

Makefile  README.md  component/  driver/  service/  vendor/  worker/

我想在所有测试文件上运行go test,例如除{vendor}中的测试文件之外的foobar_test.go个文件。我最接近成功的是go test ./...,但其中包括供应商测试文件。

我在文档中看到您可以将正则表达式传递给-run选项,但我无法正常工作。例如,我尝试了go test ./*,但我得到了一堆can't load package errors

最好的方法是什么?

2 个答案:

答案 0 :(得分:33)

-run模式仅与测试标识符(不是文件名)匹配;原则上你可以这样做:

go test -run TestFoo

但是当您必须将Foo添加到您可能不想要的所有测试功能名称时。

通常的做法是:

go test $(go list ./... | grep -v /vendor/)

a lengthy discussion at GitHub about it。最终改变了after another lengthy discussion

Go 1.9开始,vendor目录会自动排除。现在,您可以直接输入:

go test ./...

答案 1 :(得分:3)

  

cmd/go: exclude vendor dir from matching ... #19090

     

[go] 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

尝试在提示处运行go test ./...或等待Go1.9。