Golang 测试:"no test files"

Golang testing: "no test files"

我正在我的包目录中创建一个名为 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)
        }
    }
}

每当我尝试 运行 它的输出是

exampleFolder[no test files] 

这是我的环境

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"

任何帮助将不胜感激。谢谢!!

包含测试的文件应称为 name_test,并带有 _test 后缀。他们应该与他们正在测试的代码放在一起。

到 运行 测试递归调用 go test -v ./...

来自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.

可能您在根包中没有任何测试文件并且运行 go test -v不测试子包,只测试根包。

例如

.
├── Dockerfile
├── Makefile
├── README.md
├── auth/
│   ├── jwt.go
│   ├── jwt_test.go
├── main.go

如您所见,根包中没有测试文件,只有 main.go 文件。你会得到 "no test files."

解决方案是递归地测试当前工作目录中的所有包

go test -v ./...

或者如果您使用 govendor

govendor test +local

或者您可以指定要测试的包(目录)

go test -v ./packagename

或者递归测试包

go test -v ./packagename/...

_test 文件中的测试函数必须以前缀 "Test"

开头

好:

func TestName (

差:

func NameTest (

此函数将不会作为测试执行,结果会报告错误

我遇到了同样的问题。 除了之前的答案之外,我还发现无法 运行 测试你的包的文件夹名称是否为 testing.

的问题

以下问题的终端演示:

testing 文件夹名称:

~/go/src/testing$ go test
?       testing [no test files]

没有testing文件夹名称:

~/go/src/testing_someothername$ go test
PASS
ok      testing_someothername   0.089s

对我来说很有帮助

我遇到了同样的问题。我通过附加各种包来修复它们

去测试-v ./ ./2ndpackage ./3rdpackage ./4thpackages

这解决了问题。

另外我在测试关键字和函数名之间添加了“_” Test_FuncName

到运行 所有测试都使用下面的命令

> go test ./...
//For verbose output use -v flag
> go test -v ./...