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

Dan*_*obe 16 testing packages go

我的项目文件夹包含:

Makefile  README.md  component/  driver/  service/  vendor/  worker/
Run Code Online (Sandbox Code Playgroud)

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

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

最好的方法是什么?

Mar*_*oij 34

-run图案仅针对测试标识符(而不是文件名)匹配; 原则上你可以这样做:

go test -run TestFoo
Run Code Online (Sandbox Code Playgroud)

但是当你必须添加Foo你可能不想要的所有测试功能名称时.

通常的方法是:

go test $(go list ./... | grep -v /vendor/)
Run Code Online (Sandbox Code Playgroud)

在GitHub上它了长时间的讨论.经过另一次冗长的讨论,它最终发生了变化.

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

go test ./...
Run Code Online (Sandbox Code Playgroud)


pet*_*rSO 6

cmd/go:从匹配中排除供应商目录...#19090

[go] cmd/go:从...匹配中排除供应的软件包

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

去/去/fa1d54c2edad607866445577fe4949fbe55166e1

commit    fa1d54c2edad607866445577fe4949fbe55166e1
Wed Mar 29 18:51:44 2017 +0000
Run Code Online (Sandbox Code Playgroud)

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