dongyi5425 2015-08-03 18:21
浏览 44
已采纳

我可以在旅途中创建共享的测试实用程序吗?

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条回答 默认 最新

  • duanjunjie0196 2015-08-03 18:26
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面