Join us
Unit testing is the most important thing in developing an application, which aims to minimize the occurrence of errors, therefore software developers are obliged to master it. In this article, we will write unit tests in the Golang programming language using the unit testing package provided by Golang.
Let’s get started!
What are Unit Tests?
In computer programming, unit testing is a software testing method by which individual units of source code — sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures — are tested to determine whether they are fit for use
Developers use unit testing to validate individual functions, methods, modules, and packages. Unit testing aids in the discovery and elimination of issues early in the development cycle, as well as the prevention of regressions during refactoring. A decent unit test may also act as documentation for new developers on the project.
Goals
In this article, it is hoped that readers will get an insight into how to do basic testing on the Golang programming language using the standard package, namely “testing”.
Step 1: project structure
In a project belonging to someone else, surely you have encountered a file that has the prefix _test.go, right? and always in the same directory as the main file to be tested. At this time we will first create the project structure as below:
don’t forget to run go mod init to initialize the project on Golang.
Step 2: make simple func
We will create a simple function that we will test later, the name of the function is Calculate(). This Calculate() function has the aim of adding a number and adding 2, it’s quite easy isn’t it, let’s make the code as below:
Step 3: Create a test file
after we create the Calculate() function, the next step is that we will create a test file for the function. Create a new file called main_test.go and put it in the same directory, follow the code below:
Step 4: Running Tests
After we have created the test file, it’s time for us to try to run the testing command by calling the go test command on the terminal or CMD, as below:
If the process is successful then the display of the test process will be as shown below:
easy isn’t it? This is more or less how to make a test in the Golang programming language.
Table Driven Testing
After we succeeded in creating a test file in the previous Calculate() function, there is actually another way to make the test code better and dynamic, namely by creating an array model. You can see an example of this code below:
With the above code, we can declare the input and expected values for each test case in the data array above. After you add it, try running the test one more time:
Conclusion
in this article, it can be concluded that writing unit tests is very important for the development process so that it can help carry out test code that has been made according to expectations or not, in the next stage maybe I will discuss code coverage on unit tests that we have made previously. If you need the source code above, just check my GitHub here https://github.com/bangadam/go-test-introduction
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.