简体   繁体   English

导出结构仅用于在Golang中进行测试

[英]Export structure only for testing in Golang

I have a utility package that many other packages use. 我有一个许多其他包使用的实用程序包。 I also created some test structures that implement those interfaces. 我还创建了一些实现这些接口的测试结构。 And I put them in interfaces_test.go 我把它们放在interfaces_test.go

I'd like to be able to import those test structures in other packages in my *_test.go files. 我希望能够在我的*_test.go文件中的其他包中导入这些测试结构。

I saw something like that in http://golang.org/src/pkg/os/export_test.go but whatever I try I get an error similar to this one: 我在http://golang.org/src/pkg/os/export_test.go中看到了类似的内容,但无论我尝试什么,我都会收到与此类似的错误:

go test something/mypackage
mypackage/ant_lat_lon_test.go:46: undefined: rutl.TestAntenner
FAIL    something/mypackage [build failed]

Is there a way to do it? 有办法吗?

Files matching *_test.go are only compiled when testing the package they are part of. 匹配*_test.go文件仅在测试它们所属的包时编译。 If you are testing package A that uses package B, you won't have access to the _test.go code from package B. 如果您正在测试使用程序包B的程序包A,则无法访问程序包B中的_test.go代码。

So the two options are: 所以这两个选项是:

  1. Always compile the test support code into package B. 始终将测试支持代码编译到包B中。
  2. If the test support code only depends on the exported interface of B, consider splitting it out into a separate package. 如果测试支持代码仅依赖于B的导出接口,请考虑将其拆分为单独的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM