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

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

问题描述

我在 Go 中看到了几种不同的测试包命名策略,想知道每种策略的优缺点以及我应该使用哪一种.

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.

策略 1:

文件名:github.com/user/myfunc.go

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

package myfunc

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

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

package myfunc

有关示例,请参见 bzip2.

策略 2:

文件名:github.com/user/myfunc.go

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

package myfunc

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

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

package myfunc_test

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

参见 wire 的示例.

策略 3:

文件名:github.com/user/myfunc.go

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

package myfunc

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

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

package myfunc_test

import (
    . "myfunc"
)

参见 字符串 的示例.

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

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?

推荐答案

您列出的三种策略之间的根本区别在于测试代码是否与被测代码在同一个包中.决定在测试文件中使用 package myfunc 还是 package myfunc_test 取决于您是否要执行 白盒黑盒测试.

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.

在一个项目中同时使用这两种方法并没有错.例如,您可以拥有 myfunc_whitebox_test.gomyfunx_blackbox_test.go.

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.

  • 黑盒测试:使用 package myfunc_test,这将确保您只使用 导出的标识符.
  • 白盒测试:使用 package myfunc 以便您可以访问未导出的标识符.适用于需要访问非导出变量、函数和方法的单元测试.
  • 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.
  • 策略 1:myfunc_test.go 文件使用 package myfunc — 在这种情况下,myfunc_test.go<中的测试代码/code> 将与在 myfunc.go 中测试的代码在同一个包中,在本例中为 myfunc.
  • 策略 2: 文件 myfunc_test.go 使用 package myfunc_test — 在这种情况下,myfunc_test.go<中的测试代码/code>将被编译为一个单独的包,然后与主测试二进制文件链接并运行."[来源:test.go 源代码中的第 58-59 行]
  • 策略 3: 文件 myfunc_test.go 使用 package myfunc_test 但使用点符号导入 myfunc —这是策略 2 的变体,但使用点符号导入 myfunc.
  • 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天全站免登陆