Golang testing: "no test files"
Asked Answered
Q

9

134

I'm creating a simple test within my package directory called reverseTest.go

package main

import "testing"

func TestReverse(t *testing.T) {
    cases := []struct {
        in, want string
    }{
        {"Hello, world", "dlrow ,olleH"},
        {"Hello, 世界", "界世 ,olleH"},
        {"", ""},
    }

    for _, c := range cases {
        got := Reverse(c.in)
        if got != c.want {
            t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
        }
    }
}

whenever i try to run it the output is

exampleFolder[no test files] 

this is my go env

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/juan/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"

Any help will be greatly appreciated. Thanks!!

Quitrent answered 30/1, 2015 at 16:37 Comment(1)
for Go testing your Test file should have **_test.go example.go should have correspondingexample_test.goRaymonderaymonds
N
192

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 named TestXXX with signature func (t *testing.T). The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed.

Nahamas answered 30/1, 2015 at 16:40 Comment(3)
my file did end in _test.go. fixing the issue for me required removing a re-definition of TestMain in the new test file that I added to the package. I also had to change directory into the package folder itself and run go test from there instead of running it from the root directoryLaureen
@thwd Test methods don't need to be (and many times shouldn't be) in the same package as the tested code.Drusilla
The file containing test can't be named only _test.go. It should be at least one character before underscore like a_test.go.Rhodia
R
132

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/...
Reathareave answered 7/1, 2017 at 0:37 Comment(0)
H
22

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

Hampden answered 13/6, 2018 at 10:10 Comment(1)
To add to this it has to be Test followed by an capital letter or underscore etc. You can't have TestmyFunction(). This bit me.Aswan
C
7

To run all the tests use below command

> go test ./...
//For verbose output use -v flag
> go test -v ./...
Cayla answered 19/1, 2022 at 3:29 Comment(0)
W
4

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

Waggle answered 15/6, 2019 at 20:23 Comment(0)
A
0

I faced same problem. I fixing them by appending various packages

go test -v ./ ./2ndpackage ./3rdpackage ./4thpackages

this solved the issue.

Also I added "_" between Test keyword and function name Test_FuncName

Arginine answered 31/10, 2021 at 13:34 Comment(0)
F
0

no test files mean you need to rename your test file to reflect the file you want to test. Example main.go main_test.go

Where main.go is the file containing your code. main_test.go is the file containing your test code.

Festoon answered 16/11, 2022 at 13:37 Comment(0)
S
0

Mine was resolved by adding os.Exit(m.Run()) at the end of TestMain(m *testing.M) function.

Sharasharai answered 3/5 at 8:47 Comment(0)
T
-1

I'm also occur this question. When I remove func TestMain(t *testing.T), it works ok!

Trolley answered 28/8, 2023 at 10:49 Comment(2)
It's not clear how this answer addresses the original question. Could you add some more context?Reducer
Its likely because os.Exit(m.Run()) was missing in your TestMainSharasharai

© 2022 - 2024 — McMap. All rights reserved.