【问题标题】:How to run go test on all test files in my project except for vendor packages如何在我的项目中的所有测试文件上运行 go test,供应商包除外
【发布时间】:2017-09-16 09:24:42
【问题描述】:

我的项目文件夹包含:

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

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

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

最好的方法是什么?

【问题讨论】:

    标签: testing go packages


    【解决方案1】:

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

    go test -run TestFoo
    

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

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

    【讨论】:

      【解决方案2】:

      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。

      【讨论】:

        猜你喜欢
        • 2018-05-11
        • 2018-12-24
        • 2017-09-10
        • 2017-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多