登录
首页 >  Golang >  Go问答

测试失败 - GOPATH 中缺少测试文件

来源:stackoverflow

时间:2024-02-22 08:33:25 337浏览 收藏

本篇文章给大家分享《测试失败 - GOPATH 中缺少测试文件》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

在以下环境中:

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$

go test 给出以下错误:

code$ go test github.com/myhub/codingtest
?       github.com/myhub/codingtest   [no test files]
code$ 
code$ 
code$ 
code$ 
code$ 
code$

如何解决no测试文件错误


解决方案


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

如您所见in the documentation

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

以上就是《测试失败 - GOPATH 中缺少测试文件》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>