DEV Community

Muhammad Adam
Muhammad Adam

Posted on • Edited on

Writing Unit Test in Golang Easily

Writing Unit Test in Golang Easily

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:

project/
 - main.go
 - main_test.go
Enter fullscreen mode Exit fullscreen mode

don’t forget to run go mod init to initialize the project on Golang.

$ go mod init
Enter fullscreen mode Exit fullscreen mode

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:

$ go test
Enter fullscreen mode Exit fullscreen mode

If the process is successful then the display of the test process will be as shown below:

testing pass

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:

testing pass

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

Thanks For Reading!

Available for a new project! Let’s have a talk :
Email: bangadam.dev@gmail.com
Linkedin: https://www.linkedin.com/in/bangadam

Top comments (0)