英文:
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]
这是我的go环境:
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"
非常感谢您的帮助!
英文:
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!!
答案1
得分: 182
测试文件应该被称为name_test
,并带有_test
后缀。它们应该与它们所测试的代码放在一起。
要递归运行测试,请调用go test -v ./...
根据如何编写Go代码:
通过创建一个以
_test.go
结尾的文件,并在其中编写名为TestXXX
的函数(函数签名为func (t *testing.T)
),来编写测试。测试框架会运行每个这样的函数;如果函数调用了t.Error
或t.Fail
等失败函数,则认为测试失败。
英文:
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.
答案2
得分: 128
可能是因为你的根包中没有任何测试文件,并且运行 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/...
英文:
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/...
答案3
得分: 22
你的测试函数在测试文件中必须以前缀"Test"开头。
正确的写法:
func TestName (
错误的写法:
func NameTest (
这个函数将不会被执行为测试,并报告错误
英文:
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
答案4
得分: 6
要运行所有的测试,请使用以下命令:
> go test ./...
// 若要获取详细输出,请使用 -v 标志
> go test -v ./...
英文:
To run all the tests use below command
> go test ./...
//For verbose output use -v flag
> go test -v ./...
答案5
得分: 3
我遇到了同样的问题。
除了之前的答案之外,我发现一个问题,如果你的包的文件夹名字是testing
,那么无法运行测试。
下面是问题的终端演示:
使用testing
文件夹名字:
~/go/src/testing$ go test
? testing [没有测试文件]
不使用testing
文件夹名字:
~/go/src/testing_someothername$ go test
PASS
ok testing_someothername 0.089s
在我的情况下,这很有帮助。
英文:
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
答案6
得分: 0
我遇到了同样的问题。我通过添加各种包来解决它们。
go test -v ./ ./2ndpackage ./3rdpackage ./4thpackages
这解决了问题。
另外,我在测试函数名的Test关键字和函数名之间添加了"_"。
Test_FuncName
英文:
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
答案7
得分: 0
“no test files”意味着你需要重命名你的测试文件以反映你想要测试的文件。
示例
main.go
main_test.go
其中,main.go是包含你的代码的文件,main_test.go是包含你的测试代码的文件。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论