dqkxo44488 2019-06-01 06:20
浏览 93

如何打包golang测试助手代码?

I have some test helper code in my golang library that I want to use when testing in various subpackages. However, I've hit a snag:

outer
|
+- test_helpers_test.go
|
+- inner
   |
   +- something.go
   +- something_test.go

To use the code in test_helpers_test.go, I have to import the outer package. But when I import the outer package from something_test.go, it complains "import cycle not allowed in test"

So I tried making a package for the shared test helpers:

outer
|
+- test
|  |
|  +- test_helpers_test.go
|
+- inner
   |
   +- something.go
   +- something_test.go

And now it complains "no non-test Go files in /home/karl/Projects/outer/test"

I don't want to call it test_helpers.go because it's part of my testing code, not my library code. I don't want to ship that code in the library.

How do I solve this?


Update: I can work around the issue by creating a dummy.go file in the test directory, but now there's a new problem: Importing a package DOESN'T import its test code! So now I get: ./something_test.go:12:2: undefined: test.AssertDoesPanic

  • 写回答

2条回答 默认 最新

  • doubipiao1611 2019-06-01 06:24
    关注

    Following go issue 8279, I have seen dummy files added, as in tommie/acme-cli commit 479f8c7

    outer/outer.go
    
    // +build ignore+
    package outer
    

    See if that could help here, as a workaround.


    As commented above, using a test helper code in an internal package (from Go 1.4+) is another option, since no client from this project would be able to access said helper. See design document.
    You can see it used in a typical Go project layout.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算