【发布时间】:2019-12-03 22:07:13
【问题描述】:
我在同一个包中有 2 个文件。
# my_package1/my_file1.go
func myFunc1() {
//....
}
# my_package1/my_file1_test.go
type MyPackageSuite struct {
suite.Suite
}
func (s *MyPackageSuite) MyTest1() {
//...............
res1 := myFunc1()
//...............
}
我运行测试go test my_package1/my_file1_test.go -v,它返回undefined: myFunc1
但它们在同一个包中。为什么会出错?如何解决?公开该方法不是我想要的。
更新1:
$ ls webhook
doc.go webhook.go webhook_test.go
然后
$ go test webhook
can't load package: package webhook: malformed module path "webhook": missing dot in first path element
$ go test webhook/webhook
can't load package: package webhook/webhook: malformed module path "webhook/webhook": missing dot in first path element
$ go test webhook/webhook.go
? command-line-arguments [no test files]
$ go test webhook/webhook_test.go
# command-line-arguments [command-line-arguments.test]
webhook/webhook_test.go: undefined: myFunc1
FAIL command-line-arguments [build failed]
FAIL
【问题讨论】:
-
不要使用文件名运行测试。要么在包下 cd 并运行 go test,要么运行 go test
. -
@BurakSerdar 查看我的更新
-
如果它抱怨导入周期,你导入的是同一个包。不要那样做。如果您使用的是 go 模块,请使用完整的包路径:go test moduleName/webhook。如果没有,请使用相对包目录:go test ./webhook。 go test webhook 尝试测试一个名为
webhook的包,因为它没有主机名作为第一个组件,所以它假定它是一个 stdlib 包。 -
@BurakSerdar 从包目录运行
go test导致import cycle not allowed in test。否则,不使用私有方法进行此测试,它可以工作。也就是说,如果我不使用私有方法的测试,这可以工作 -->go test webhook/webhook_test.go -
如果你想测试一些
package p的导出边界并且有一个p_test.go 文件将自己声明为package p_test那么你不能从p 访问未导出的东西。这是由 设计 并且不能被丑陋的伎俩规避。别再这样了。