我可以创建共享的测试工具吗?

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

Can I create shared test utilities?

问题

我想知道在Go语言中是否可以在不同的包之间共享测试工具代码。具体来说,我正在编写一个TCP服务器,将被用于处理不同消息类型的多个处理程序,并且希望重用一组常见的测试工具。

主要的TCP服务器代码位于mypkg/tcpserver包中:

mypkg/tcpserver/tcp_server.go
mypkg/tcpserver/tcp_server_test.go
mypkg/tcpserver/testutils_test.go

testutils_test.go代码旨在成为一个共享库,可以被mypkg/tcpserver和其他包使用,以便在它们的测试中设置测试服务器和客户端。例如,在handler子包中,我有:

mypkg/tcpserver/handler/handler.go
mypkg/tcpserver/handler/csv_handler.go
mypkg/tcpserver/handler/csv_handler_test.go
mypkg/tcpserver/handler/delim_handler.go
mypkg/tcpserver/handler/delim_handler_test.go

handler/*_test.go文件都导入了mypkg/tcpserver,但它们无法访问mypkg/tcpserver/testutils_test.go中定义的测试工具:

$ wgo test mypkg/tcpserver/handler
# mypkg/tcpserver/handler
src/mypkg/tcpserver/handler/csv_handler_test.go:37: undefined: tcpserver.CreateTestServer
src/mypkg/tcpserver/handler/csv_handler_test.go:40: undefined: tcpserver.TestSender
FAIL	mypkg/tcpserver/handler [build failed]

看起来测试可以导入其他包,但无法访问这些包中定义的测试代码?这是有意为之吗?如果是这样,是否有一种惯用的方法可以在Go语言中在不同的包之间共享测试工具?

英文:

I'm wondering whether it's possible to share test utility code across packages in go. Specifically, I'm writing a TCP server that will be used by multiple handlers for different message types, and want to reuse a set of common test utils.

The main TCP server code is in mypkg/tcpserver:

mypkg/tcpserver/tcp_server.go
mypkg/tcpserver/tcp_server_test.go
mypkg/tcpserver/testutils_test.go

The testutils_test.go code is intended to be a shared library that can be used by mypkg/tcpserver and other packages to set up a test server and client for their tests. For example in the handler subpackage I have:

mypkg/tcpserver/handler/handler.go
mypkg/tcpserver/handler/csv_handler.go
mypkg/tcpserver/handler/csv_handler_test.go
mypkg/tcpserver/handler/delim_handler.go
mypkg/tcpserver/handler/delim_handler_test.go

The handler/*_test.go files all import mypkg/tcpserver, but they are unable to access the test utilities defined in mypkg/tcpserver/testutils_test.go:

$ wgo test mypkg/tcpserver/handler
# mypkg/tcpserver/handler
src/mypkg/tcpserver/handler/csv_handler_test.go:37: undefined: tcpserver.CreateTestServer
src/mypkg/tcpserver/handler/csv_handler_test.go:40: undefined: tcpserver.TestSender
FAIL	mypkg/tcpserver/handler [build failed]

It appears that tests can import other packages, but not test code defined within those packages? Is this the intent? If so, is there an idiomatic way to share test utilities across packages in go?

答案1

得分: 30

创建一个包,其中包含非 _test.go 文件中的测试工具。这种方法的几个示例是 httptestiotest

英文:

Create a package containing the testing utilities in non _test.go files. A couple of examples of this approach are httptest and iotest.

答案2

得分: 9

如果由于文档、依赖等原因无法创建一个测试工具包,可以尝试将测试工具移到一个单独的Go文件中,并在文件顶部添加注释//go:build testing。然后可以使用go test -tags testing ./...来运行测试。这些代码不会包含在go build生成的二进制文件中。

关于Go构建标签的更多信息,请参考:https://pkg.go.dev/cmd/go#hdr-Build_constraints

英文:

If you can't create a testing utilities package (due to docs, dependencies, etc.), try to move testing utiities to a single go file, and add comment //go:build testing on the top of the file. Then you can run tests with go test -tags testing ./.... This codes won't be contained in go build binaries.

See more about go build tags: https://pkg.go.dev/cmd/go#hdr-Build_constraints

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

发表评论

匿名网友

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

确定