Unit testing 仅为_test.go文件定义结构

Unit testing 仅为_test.go文件定义结构,unit-testing,testing,go,Unit Testing,Testing,Go,我有以下文件树结构: -app/ ---tool/ -----/tool_test.go -----/tool.go -----/proto/proto.go -----/proto/proto_test.go 我需要在tool\u test.go和proto\u test.go中使用(虚拟)结构实现接口: type DummyRetriever struct{} func (dummy *DummyRetriever) Retrieve(name string) (string, erro

我有以下文件树结构:

-app/
---tool/
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go
我需要在
tool\u test.go
proto\u test.go
中使用(虚拟)结构实现接口:

type DummyRetriever struct{}

func (dummy *DummyRetriever) Retrieve(name string) (string, error) {
  return "", nil 
}
如果我仅在
tool\u test.go
中定义它,我无法在
proto\u test.go
中查看和使用它,因为_test.go文件不导出名称

我在哪里定义
DummyRetriever
,使其在两个包中都可用?
我希望避免在文件中定义它,以便名称在核心(非测试)包中也可见。

如果您需要在两个不同的包中使用mock,那么mock不能存在于测试文件中(以
\u test.go
结尾的文件)

如果您不关心mock在哪里使用,那么只需创建一个
mock
包并放在那里

-app/
---tool/
-----mock/
-------/dummyretriever.go
-------/othermock.go
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go
如果您只希望从该包或其子代使用模拟,请将其放入
内部
包中

-app/
---tool/
-----internal/
-------/dummyretriever.go
-------/othermock.go
-----/tool_test.go
-----/tool.go
-----/proto/proto.go
-----/proto/proto_test.go

如果不需要测试未公开的函数,可以在所有测试中使用
\u test


编辑:我不理解这些反对票

出于这个原因,其中一种做法是将mock放在一个单独的包中(参见#3)。谢谢@zerkms,基本上与MahlerFive的答案相同。。。。