go test 错误 - 没有测试文件 - GOPATH 中的错误

ove*_*nge -4 go

在以下环境中:

code$ pwd
/home/user1/code
code$ echo $GOPATH
/home/user1/golib:/home/user1/code
code$ ls
bin  pkg  src 
code$ ls src/github.com/myhub/codingtest/
main.go  test_main.go
code$ 
code$ 
code$ 
code$ 
code$ 
code$ cat src/github.com/myhub/codingtest/test_main.go 
package main

import "testing"

func TestSplit(t *testing.T) {
        gotAllButLast, gotLast := split(2013)
        wantAllButLast := 201
        wantLast := 3
        if gotAllButLast != wantAllButLast {
                t.Errorf("got %d but expected %d", gotAllButLast, wantAllButLast)
        }
        if wantLast != gotLast {
                t.Errorf("got %d but expected %d", gotLast, wantLast)
        }
}
code$ 
code$ 
code$ 
code$ 
code$ 
code$ cat src/github.com/myhub/codingtest/main.go 
package main

// Spit n into all but its last digit and its last digit
func split(n int) (int, int) {
        return n / 10, n % 10
}

func main() {

}
code$ 
Run Code Online (Sandbox Code Playgroud)

go test 给出以下错误:

code$ go test github.com/myhub/codingtest
?       github.com/myhub/codingtest   [no test files]
code$ 
code$ 
code$ 
code$ 
code$ 
code$ 
Run Code Online (Sandbox Code Playgroud)

如何解决no test files error

Moi*_*ioM 5

要解决您的错误,您需要将 test_main.go 重命名为 main_test.go

正如您在文档中看到

要编写新的测试套件,请创建一个名称以 _test.go 结尾的文件