使用Go语言进行测试的正确软件包命名 [英] Proper package naming for testing with the Go language
问题描述
我在Go中看到了几种不同的测试包命名策略,并想知道每种策略的优缺点,以及我应该使用哪一种策略。 策略1:
文件名:github.com/user/myfunc.go
包myfunc
测试文件名:github.com/user/myfunc_test .go
package myfunc
例如,请参阅 bzip2 。
策略2:
文件名:github.com/user/myfunc.go
包myfunc
测试文件名:github.com/user/myfunc_test.go
包myfunc_test
import(
github.com/user/myfunc
)
请参阅线一>例如。 策略3: 文件名:github。 com / user / myfunc.go 测试文件名称:github.com/user/myfunc_test.go 请参阅string 作为例子。 Go标准库似乎使用策略1和策略2的混合我应该使用哪三种?在我的测试包中添加了 您列出的三种策略的根本区别在于测试代码是否与代码正在测试中。在测试文件中使用 在项目中使用这两种方法没有任何问题。例如,您可以使用 I have seen several different test package naming strategies within Go and wanted to know what pros and cons of each are and which one I should use. Strategy 1: File name: github.com/user/myfunc.go Test file name: github.com/user/myfunc_test.go See bzip2 for an example. Strategy 2: File name: github.com/user/myfunc.go Test file name: github.com/user/myfunc_test.go See wire for an example. Strategy 3: File name: github.com/user/myfunc.go Test file name: github.com/user/myfunc_test.go See strings for an example. The Go standard library seems to use a mixture of strategy 1 and 2. Which of all three should I use? It's a pain appending The fundamental difference between the three strategies you've listed is whether or not the test code is in the same package as the code under test. The decision to use There's nothing wrong with using both methods in a project. For instance, you could have 这篇关于使用Go语言进行测试的正确软件包命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
package myfunc
包myfunc_test
导入(
。 myfunc
)
package * _test
是一种痛苦,因为这意味着我无法测试我的包私有方法,但是可能存在我不知道的隐藏优势? 包myfunc
或 package myfunc_test
的决定取决于您是否要执行white-box 或 black- box 测试。
myfunc_whitebox_test.go
和 myfunx_blackbox_test.go
。
测试代码包比较
package myfunc_test
,这将确保您只使用导出的标识符。
软件包myfunc
到非导出的标识符。适用于需要访问非导出变量,函数和方法的单元测试。
问题列表中的策略比较
myfunc_test.go
使用 package myfunc
- 在这种情况下, myfunc_test.go
中的测试代码将与 myfunc.go
,在本例中 myfunc
。
myfunc_test.go
使用 package myfunc_test
- 在这种情况下, code> myfunc_test.go 将被编译为一个单独的包,然后与主测试二进制文件链接并运行。 [源代码: test.go 源代码中的第58-59行]
myfunc_test.go
使用软件包myfunc_test
,但导入 myfunc
使用点符号 - 这是策略2的变体,但使用点符号来导入 myfunc
。
package myfunc
package myfunc
package myfunc
package myfunc_test
import (
"github.com/user/myfunc"
)
package myfunc
package myfunc_test
import (
. "myfunc"
)
package *_test
to my testing packages as it means I can't test my package private methods but maybe there is a hidden advantage I am not aware of? package myfunc
or package myfunc_test
in the test file depends on whether you want to perform white-box or black-box testing.myfunc_whitebox_test.go
and myfunx_blackbox_test.go
. Test Code Package Comparison
package myfunc_test
, which will ensure you're only using the exported identifiers.package myfunc
so that you have access to the non-exported identifiers. Good for unit tests that require access to non-exported variables, functions, and methods.Comparison of Strategies Listed in Question
myfunc_test.go
uses package myfunc
— In this case the test code in myfunc_test.go
will be in the same package as the code being tested in myfunc.go
, which is myfunc
in this example.myfunc_test.go
uses package myfunc_test
— In this case the test code in myfunc_test.go
"will be compiled as a separate package, and then linked and run with the main test binary." [Source: Lines 58–59 in the test.go source code]myfunc_test.go
uses package myfunc_test
but imports myfunc
using the dot notation — This is a variant of Strategy 2, but uses the dot notation to import myfunc
.