vendredi 31 mai 2019

How do I package golang test helper code?

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?

Aucun commentaire:

Enregistrer un commentaire