Bets approach for test helper packages within same project

268 views
Skip to first unread message

josvazg

unread,
Aug 4, 2023, 2:01:29 PM8/4/23
to golang-nuts
We are working on a project that requires some test helpers and mocks that, ideally, should:

- Helpers should be accessible by all testing code, unit tests, integration or e2e. 
      - Note unit tests live along side normal code in their *_test.go files.
      - The rest of tests will be on a separated test/ folder using a built tag to avoid to be compiled in with regular code.

- Helpers should not be part of the default exported module code.

- Helpers do not need to be accessible by the regular code, just the test code.

- Helpers might need tests themselves.

How can we achieve that in the simplest or cleanest way? We do not want to create yet another project for just that, as it is only to be used within this project.

I was thinking about a build tag for such code and just put it on a regular folder, such as testsupport/. But that would break unit tests that need that unless you setup the build tag, which feels weird.

Another option is to place the test support and the test that use them under the same build tag and folder as test/. And run them separately with the build tag. That creates a special category of unit tests, when they are not integration or e2e but need supporting code from those test/support/ packages.

Maybe use a module at testsupport/ separate from the rest of the code and use it as a library from test code?

Any better options?
Or maybe there is a good known solution for this I should know about.

Jose

Nagaev Boris

unread,
Aug 5, 2023, 2:05:52 AM8/5/23
to josvazg, golang-nuts
Hi,

You can put test helpers to a separate package (directory), say
testhelper/ without a build tag. Code in that directory will use
regular code and it will have its own unit tests in testhelper/
directory. When you build your production binary, it won't include
such code, because it is not imported by any production code (directly
or indirectly). At the same time, you can import the "testhelper"
package from your integration tests as usual.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ca983d85-b5f5-423d-b949-71fa41b414ccn%40googlegroups.com.



--
Best regards,
Boris Nagaev

josvazg

unread,
Aug 7, 2023, 6:48:24 AM8/7/23
to golang-nuts
Interesting! Thanks!

I was assuming wrongly the module was the unit of compilation.

If any package not imported by production code is not included, then that also means that build tags are not necessary for the integration and end to end test folders. Correct?

Any pointers on where can I read more about this?

Jose

TheDiveO

unread,
Aug 7, 2023, 7:38:40 AM8/7/23
to golang-nuts

josvazg

unread,
Aug 7, 2023, 9:21:33 AM8/7/23
to golang-nuts
Many thanks!

Replying to myself as well:
Build tags are still useful to separate away non unit tests that require some ad-hoc setup to be run. That way doing "go test ./..." on the project root works as expected.

On the other hand, integration or end to end tests that require some setup or external dependencies, must be run by passing the required build tags explicitly.

Jose

Reply all
Reply to author
Forward
0 new messages