Golang testing: "no test files"
Files containing tests should be called name_test
, with the _test
suffix. They should be alongside the code that they are testing.
To run the tests recursively call go test -v ./...
From How to Write Go Code:
You write a test by creating a file with a name ending in
_test.go
that contains functions namedTestXXX
with signaturefunc (t *testing.T)
. The test framework runs each such function; if the function calls a failure function such ast.Error
ort.Fail
, the test is considered to have failed.
It's possible you don't have any test files in the root package and running go test -v
does not test sub-packages, only the root package.
For example
.
├── Dockerfile
├── Makefile
├── README.md
├── auth/
│ ├── jwt.go
│ ├── jwt_test.go
├── main.go
As you see there are no test files in the root package, only the main.go file. You will get "no test files."
The solution is to test all packages within the current working directory, recursively
go test -v ./...
Or if you use govendor
govendor test +local
Or you can specify which package (directory) to test
go test -v ./packagename
Or test a package recursively
go test -v ./packagename/...
Your test function within your _test file must start with the prefix "Test"
GOOD:
func TestName (
BAD:
func NameTest (
This function will not be executed as a test and results with the reported error
I faced same problem.
In addition to previous answers i find an issue when impossible to run test if your package's folder name is testing
.
Terminal demonstration of the issue below:
with testing
folder name:
~/go/src/testing$ go test
? testing [no test files]
without testing
folder name:
~/go/src/testing_someothername$ go test
PASS
ok testing_someothername 0.089s
In my case it was helpful