Golang Tips & Tricks #6 - the _test package

Testing is one of the hardest stuff in programming. Today trick will help you organize your tests and the production code.

Let’s assume you have a package called orders. When you want to separate the package for tests from the production code you can create a new folder and write tests there. It will work but there’s a more clearer way - put your tests to the folder with you package but suffix the package’s name in tests with _test.

order.go # package orders
order_test.go #package orders_test

I use this approach a lot and it helps to keep both prod and test code together but can can test the production code like from an external package. I hope you’ll find it useful.

golang   tipstricks   tests
Published in: Golang
Did you like the post?
or

About the author

Hi! My name is Bartek. I'm a developer since 2011. I love to learn by sharing the knowledge. You can find me on Twitter or Linkedin. I'm a backend engineer at Brainly.

Recent posts

I want to help you become a developer

That's why I can let you know about new blogposts on your email

You can find me

You May Also Enjoy

Golang Tips & Tricks #5 - blank identifier in structs 22 Jul 2019

While working with structures, there’s a possibility to initialize the structure without providing the keys of fields. type SomeSturct struct { FirstField string SecondField bool } // ... my...
golang   tipstricks
read more...

Golang Tips & Tricks #4 - internal folders 25 Mar 2019

While developing a library, we create a directory structure to keep the code organized. However, some exported functions or struct should not be used by users of the library. The achieve that, call...
golang   tipstricks
read more...

Golang Tips & Tricks #3 - graceful shutdown 18 Mar 2019

In the microservices’ world, one thing what’s worth considering is a graceful shutdown. This is important to not lose data while shutting down a container. The container orchestrator like Kubernete...
golang   tipstricks
read more...