测试内部函数

huangapple go评论104阅读模式
英文:

Go: test internal functions

问题

假设我有一个类型MyType,其中有一个私有方法(mt *MyType) private(),它位于mypackage包中。
我还有一个名为tests的目录,我想在其中存储我的包的测试。tests/mypackage_test.go的内容如下:

package mypackage_test

import (
	"testing"
	"myproj/mypackage"
)

func TestPrivate(t *testing.T) {
    // 一些测试代码
}

然而,当我运行go test时,我会得到cannot refer to unexported field or method mypackage.(*MyType).private)错误。我查了一下谷歌,发现以小写字母开头的函数无法在其它包中访问(而大写字母开头的函数可以自由地从测试中调用)。

我还在某个地方读到,将<...>_internal_test.go添加到测试文件中可以解决我的问题,就像这样(tests/mypackage_internal_test.go):

package mypackage

import (
	"testing"
)

func TestPrivate(t *testing.T) {

    mt := &MyType{}
    // 一些测试代码
}  

但是这样我只会得到undefined: MyType错误。所以,我的问题是:如何测试内部/私有方法?

英文:

Suppose I have a type MyType with a private method (mt *MyType) private() in a package mypackage.
I also have a directory tests, where I want to store tests for my package. This is how tests/mypackage_test.go looks like:

package mypackage_test

import (
	&quot;testing&quot;
	&quot;myproj/mypackage&quot;
)

func TestPrivate(t *testing.T) {
    // Some test code
}

However, when I run go test I get the cannot refer to unexported field or method my package.(*MyType).&quot;&quot;.private) error. I've googled a bit and found out that functions starting with lower case can not be seen outside their own package (and this seems to be true, 'cause upper case functions are freely callable from the tests).

I also read somewhere that adding &lt;...&gt;_internal_test.go to the test file could solve my problem like this (tests/mypackage_internal_test.go):

package mypackage

import (
	&quot;testing&quot;
)

func TestPrivate(t *testing.T) {

    mt := &amp;MyType{}
    // Some test code
}  

But with this I only get undefined: MyType. So, my question: how can I test internal/private methods?

答案1

得分: 16

为什么你把测试放在不同的包中?Go的测试机制使用_test作为测试文件的后缀,这样你就可以将测试放在与实际代码相同的包中,避免了你所描述的问题。将测试放在单独的包中不符合Go的惯例。不要试图违背Go的约定,这样做不值得,而且你大多数情况下会失败。

英文:

Why do you place your tests in a different package? The go testing mechanism uses _test as a suffix for test files so you can place tests in the same packages as the actual code, avoiding the problem you describe. Placing tests in a separate package is not idiomatic Go. Do not try to fight the Go conventions, it's not worth the effort and you are mostly going to lose the fight.

答案2

得分: 3

Go坚持认为同一文件夹中的文件属于同一个包,除了_test.go文件。将测试代码移出包可以让你像真正的包用户一样编写测试。你不能操纵内部细节,而是专注于暴露的接口,并始终考虑可能添加到API中的任何噪音。

并且:

如果你确实需要对一些内部进行单元测试,可以创建另一个文件,后缀为_internal_test.go。内部测试必然比接口测试更脆弱,但它们是确保内部组件行为良好的好方法,如果你进行测试驱动开发,它们尤其有用。

来源:https://medium.com/@matryer/5-simple-tips-and-tricks-for-writing-unit-tests-in-golang-619653f90742

关于如何在Go语言项目中组织测试,有不同的观点,我建议你阅读上述博客。

英文:

Go insists that files in the same folder belong to the same package, that is except for _test.go files. Moving your test code out of the package allows you to write tests as though you were a real user of the package. You cannot fiddle around with the internals, instead you focus on the exposed interface and are always thinking about any noise that you might be adding to your API.

And:

If you do need to unit test some internals, create another file with _internal_test.go as the suffix. Internal tests will necessarily be more brittle than your interface tests — but they’re a great way to ensure internal components are behaving, and are especially useful if you do test-driven development.

Source: https://medium.com/@matryer/5-simple-tips-and-tricks-for-writing-unit-tests-in-golang-619653f90742

There are different opinions on how you should struct you tests within a golang project and I suggest you to read the blog above.

huangapple
  • 本文由 发表于 2015年8月24日 18:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/32180341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定