Before taking our first steps in creating unit tests in GO, it is important to bear in mind that in this article we are going to explain and present white box and black box tests. Before moving forward we recommend reading about GO – Rest API with DI.
Now, are you ready? Let’s start creating unit tests in GO.
Unit Tests in GO
Leaving that behind, let’s move on to the important aspects of GO.
Using standard GO code is always “recommended”. However, there are many good libraries that can assist us in writing unit tests quickly, and even intuitively, especially when the logic of our software is complex.
GO in a Real Case
To learn how to test with GO we are going to use a real-world example. In this article, we will be using the request of a client who wanted to build a web system for an educational establishment.
The first thing we need to do is explain the nomenclature and structure of the project. Since it is based on DDD, we will put our test cases in the same level or folder as the services, and the files must end in “_test.go” (due to a GO labeling issue).
Cases 1 and 2 of Testing in GO
How do we create the SetUp and TearDown methods in GO? Well, there is a method called TestMain(m*testing.M) which will allow us to define the calls before and after the execution of the test.
GO Test Case # 3
Bonus # 2
In closing, I hope I have managed to give a general overview of how to perform unit tests in a project in GO.
About the Author
Mauricio Bergallo is a Systems Engineer with extensive knowledge in a variety of programming languages. Great experience and understanding with all aspects of the software development life cycle.