dsaaqdz6223 2017-04-19 23:29
浏览 98
已采纳

如何对我项目中的所有测试文件(供应商软件包除外)运行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?

  • 写回答

2条回答 默认 最新

  • doulu1968 2017-04-19 23:34
    关注

    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 usual way to do this is:

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

    There's a lengthy discussion at GitHub about it. It was eventually changed after another lengthy discussion.

    Starting with Go 1.9, vendor directory is automatically excluded. Now, you can directly type this:

    go test ./...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?