DEV Community

Patryk Woziński
Patryk Woziński

Posted on

Golang - test doubles, where to keep?

#go

Hey! I am wondering where should I put my test doubles like Spy/Stub/Dummy implementations of specific interfaces. What do you think?

I'm of course asking about something that's aligned with go standard layout (internal, pkg directories etc).

In other programming languages I'm keeping these implementations in /test directory with all tests, object-mothers, object-builders... but in Golang we're often keeping tests close to the production code.

What do you think? Have you're dealing with it? :)

Have a great week!

Top comments (1)

Collapse
 
fjones profile image
FJones

The canonical answer (as implemented in the standard library), is as follows:

  • keep stubs in the same _test.go as the actual tests wherever reasonably possible
  • otherwise (e.g. if you share test stubs), place them in a package of their own, which itself contains the word "test" (see http/httptest for example)