DEV Community

Patryk Woziński
Patryk Woziński

Posted on

1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay