Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run go test on all test files in my project except for vendor packages

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?

like image 764
Daniel Kobe Avatar asked Apr 19 '17 23:04

Daniel Kobe


People also ask

How do you run all tests in project?

This should run all tests in current directory and all of its subdirectories: $ go test ./... This should run all tests for given specific directories: $ go test ./tests/... ./unit-tests/... ./my-packages/...

How do I run a specific test file in go?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.

Does go build exclude test files?

When go builds a package normally ( go build or go install ) it will ignore any files with the name pattern *_test.go . This means that object code for any packages that are only imported from those test files will not be linked into your executable.

Which command is used to run the tests in go?

After the package test finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'), the package name, and elapsed time. To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command.

What is package testing in go?

Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the "go test" command, which automates execution of any function of the form where Xxx does not start with a lowercase letter. The function name serves to identify the test routine.

Where are Go testing files located?

By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides. These files are not built by the compiler when you run the go build command, so you needn’t worry about them ending up in deployments. And as with everything in Go, the language is opinionated about testing.

What is local directory mode in go testing?

Local directory mode is when go test is called inside a directory, without any package arguments supplied. Running go test in .../go/src/github.com/yourProject/model will compile and run all the tests in that directory (i.e. model package) Note: Tests contained in other packages will not be compiled and run.

How do I write a test in go?

In this step, you will write your first test in Go. Writing tests in Go requires a test file link, and this test file must always end with _test.go. By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides.


2 Answers

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 ... wildcard excludes the ./vendor directory since Go 1.9, so you can now just run go test ./... and it won't include ./vendor.

like image 158
Martin Tournoij Avatar answered Oct 16 '22 22:10

Martin Tournoij


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

Try running go test ./... at tip or wait for Go1.9.

like image 25
peterSO Avatar answered Oct 16 '22 22:10

peterSO