使用Go语言进行测试的正确软件包命名 [英] Proper package naming for testing with the Go language

查看:140
本文介绍了使用Go语言进行测试的正确软件包命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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

  package myfunc 

测试文件名称:github.com/user/myfunc_test.go

 包myfunc_test 

导入(
。 myfunc

请参阅string 作为例子。

Go标准库似乎使用策略1和策略2的混合我应该使用哪三种?在我的测试包中添加了 package * _test 是一种痛苦,因为这意味着我无法测试我的包私有方法,但是可能存在我不知道的隐藏优势?

解决方案

您列出的三种策略的根本区别在于测试代码是否与代码正在测试中。在测试文件中使用包myfunc package myfunc_test 的决定取决于您是否要执行white-box black- box 测试。

在项目中使用这两种方法没有任何问题。例如,您可以使用 myfunc_whitebox_test.go myfunx_blackbox_test.go

测试代码包比较


  • 黑盒测试:使用 package myfunc_test ,这将确保您只使用导出的标识符

  • 白盒测试:使用软件包myfunc 到非导出的标识符。适用于需要访问非导出变量,函数和方法的单元测试。



问题列表中的策略比较




  • 策略1:文件 myfunc_test.go 使用 package myfunc - 在这种情况下, myfunc_test.go 中的测试代码将与 myfunc.go ,在本例中 myfunc

  • 策略2:文件 myfunc_test.go 使用 package myfunc_test - 在这种情况下, code> myfunc_test.go 将被编译为一个单独的包,然后与主测试二进制文件链接并运行。 [源代码: test.go 源代码中的第58-59行]

  • 策略3:文件 myfunc_test.go 使用软件包myfunc_test ,但导入 myfunc 使用点符号 - 这是策略2的变体,但使用点符号来导入 myfunc


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

package myfunc

Test file name: github.com/user/myfunc_test.go

package myfunc

See bzip2 for an example.

Strategy 2:

File name: github.com/user/myfunc.go

package myfunc

Test file name: github.com/user/myfunc_test.go

package myfunc_test

import (
    "github.com/user/myfunc"
)

See wire for an example.

Strategy 3:

File name: github.com/user/myfunc.go

package myfunc

Test file name: github.com/user/myfunc_test.go

package myfunc_test

import (
    . "myfunc"
)

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 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?

解决方案

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 package myfunc or package myfunc_test in the test file depends on whether you want to perform white-box or black-box testing.

There's nothing wrong with using both methods in a project. For instance, you could have myfunc_whitebox_test.go and myfunx_blackbox_test.go.

Test Code Package Comparison

  • Black-box Testing: Use package myfunc_test, which will ensure you're only using the exported identifiers.
  • White-box Testing: Use 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

  • Strategy 1: The file 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.
  • Strategy 2: The file 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]
  • Strategy 3: The file 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.

这篇关于使用Go语言进行测试的正确软件包命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆