DEV Community

Cover image for golang unit testing
Lakshan Dissanayake
Lakshan Dissanayake

Posted on

golang unit testing


Test driven development also known as TDD is a good software development practice followed by all over the software industry. With unit tests we can test a small pieces of our software independently what makes it "unit".


Let's assume you're making some changes to a function which was developed by someone else previously. with the developer test you might think, it is safe to deploy. But what will happen if you missed a specific test scenario? probably it will lead to a software crash if exceptions are not properly handled. panics in go's context.


If we have already developed unit tests, then we can run it to test whether we broke the code or not. It will execute a list of specific scenarios of the software and shows if some flows are broken. if there is not unit test, I highly recommend you to implement it first, then do the changes to the function.

Libraries we can use to write unit tests

I use gomock or mockery for mocking the interfaces and testify for evaluating tests

Let'a have a look how its implemented

Sms Provider

Email Provider

And suppose notification service, which validates phone numbers and email and calls sms provider and email provider to send actual email and test messages

The unit tests of the notification service can be written as the following

Top comments (0)