目前,我遇到了设计应用程序结构及其测试基础结构的问题。
以下是布局的简要概述
<GOROOT>/src/myapp/controllers/
<GOROOT>/src/myapp/controllers/account.go
...
<GOROOT>/src/myapp/models/
<GOROOT>/src/myapp/models/account.go
<GOROOT>/src/myapp/models/account_test.go
...
<GOROOT>/src/myapp/components/
<GOROOT>/src/myapp/components/comp1/
<GOROOT>/src/myapp/components/comp1/impl.go
<GOROOT>/src/myapp/components/comp1/impl_test.go
<GOROOT>/src/myapp/components/
...
<GOROOT>/src/myapp/testutil/
<GOROOT>/src/myapp/testutil/database.go
<GOROOT>/src/myapp/testutil/models.go
...问题1
myapp/testutil/models.go文件包含在models/*_test.go测试中使用的一些util函数。util函数实际上使用包myapp/models数据结构和函数。因此,我们有一个导入周期:account_test.go导入testutil包,该包反过来导入models。
这里唯一明确的解决方案是将testutil/models.go直接保存在models包的同一个包中--类似于test_utils.go,这在我看来有点笨拙。在这种情况下,deb最好的步行方式是什么?
问题2
testutil包有一些comp1的初始化(假设它是第三方服务的客户端)。当我们运行一个测试comp1/impl_test.go时,testutil包被导入,它导入comp1包,因为它负责组件的初始化。同样的循环进口地狱。将初始化移动到测试用例中的每个地方似乎是代码的重复。仍然在寻找一些优雅的解决方案..。
发布于 2013-11-19 12:36:10
问题1
如果package testutils只是提供在测试 of package module期间使用的实用程序函数,那么只需将这些函数放入models/testutils_test.go:现在,在运行models/*_test.go测试时将包含这些实用程序函数。不再有进口周期。
这是你“唯一明确的解决方案”,我看不出有什么“笨拙”。
问题2
进口周期:与上述相同。
初始化:您的comp1/impl_test.go可以提供一个func init(),因此不需要重复代码。
(标准库是测试不同内容的好来源。在测试代码中,IMHO代码重复并不是七种致命罪过之一。
https://stackoverflow.com/questions/20069994
复制相似问题