【发布时间】:2020-08-09 08:21:54
【问题描述】:
大家,我对我所看到的感到困惑;我有以下tree:
├── go.mod
├── main.go
└── server
├── server.go
└── server_integration_test.go
假设我的模块名称 (mod.go) 是 gotest。 server.go的内容:
package server
type MyStruct struct {
Hello string
}
func (m MyStruct) SayHello() string {
return m.Hello
}
server_integration_test.go的内容:
package server_integration_test
import (
"testing"
)
func TestIntegration(t *testing.T) {
t.Errorf("just gonna fail!")
}
最后是我的main.go`:
package main
import (
"fmt"
"gotest/server"
)
func main() {
my := server.MyStruct{Hello: "my-struct"}
fmt.Println("from mystruct", my.SayHello())
}
当我运行go build(或go test ./...)时,我收到以下错误:
main.go:5:2: found packages server (server.go) and server_integration (server_integration_test.go) in /tmp/gotest/server
但如果我将我的server_integration_test.go 更改为:
package server_test
// ...
一切正常。
谁能解释一下这里发生了什么?
【问题讨论】:
-
见test packages。
go test命令支持 _test.go 文件中的两个包名:目录中的包名和带有_test后缀的目录中的包名。包名server_integration_test失败,因为它不是server或server_test。 -
@CeriseLimón 将其写为答案而不是评论
标签: go go-modules go-packages go-testing