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

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 个文件,供应商包中的测试文件除外。我最接近成功的是 go test ./...,但其中包括供应商测试文件。

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

最好的方法是什么?

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

go test -run TestFoo

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

从 Go 1.9 开始,... 通配符排除了 ./vendor 目录,因此您现在可以只 运行 go test ./... 而不会包含 ./vendor .

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。