Unit testing in Golang : Part 1



Hi everyone, in this hands on blog i would like to demonstrate simple unit testing in Golang which will help you get started. Let’s take simple go file (main.go) where there is an interface and a concrete implementation that we will test in this blog lab.

Simple Interest Calculator

Let’s examine the contents of the file.

  • LoanInfo struct which is a simple model with one field interestInPercent . As the name suggests, its a model representing information about a Loan.
  • NewLoanInfo LoanInfo constructor function with basic validation.
  • Calculator interface which has one abstract method SimpleInterest , takes principal amount (USD) and tenure (Years), returns a float64 value and an error.
  • SimpleCalculator struct which has one field of type LoanInfo and the concrete implementation of SimpleInterest which has *SimpleCalculator as receiver. This enables java like Is-A relationship between Calculator and SimpleCalculator, read more here.
  • NewSimpleCalculator is constructor function for initializing SimpleCalculator.

Now we want to test the behaviour of SimpleInterest method. Let’s create a main_test.go . In this test, we will use the testing package provided by Go and my goto testing library testify.

We will use the suite package and style in this lab.

Lets examine the Anatomy of the above test file.

  • CalculatorTestSuite is the struct which defines the test suite.
  • TestCalculatorTestSuite is the function which runs the suite.
  • SetupTest is the function which runs before each test function, which we will write in the next section. It’s the Golang twin of BeforeEach from the Junit world.

Now we will add the test function which should test SimpleInterest method called TestSimpleInterest . We need SimpleCalculator Initialized for the Test. We can create a new variable in CalculatorTestSuite of Calculator interface type, Initialize it in SetupTest. Here is the updated File.

testing SimpleInterest method

In TestSimpleInterest test function, i am using suite.Equal assertion method to verify what is being returned vs what is expected. thats it, now we have written a simple test for SimpleInterest method.

Test data: I already pre-calculated the simple interest for interest 10% for 1000 USD for 3 years tenure, i.e 1300 USD.

Table Driven Tests !

What we have done in previous section is to add one basic test, However when writing production code, we will need to test different scenarios to increase the reliability of our code, for example different inputs, testing errors etc. Table driven tests can be used to test different scenarios.

In Go, I would say table driven tests are idiomatic. Let’s modify the existing test to have table driven tests and add one simple scenario.

test cases

Let’s write the assertions now.

Table driven test for SimpleInterest

Here we are iterating tests slice (array), for each test case, we are running the test. I am using suite.T() to run each test in its own test context. We can use the testify.assert package here to use the dedicated test context in assertions as well.

To complete this blog, let’s add a case to supply invalid inputs.

table driven tests with success and failure cases.

Thats it folks! Hope this is helpful for you. Let me know any questions or feedback in the comments. We will see the usage of mocks in part 2.




Software Engineer | Foodie | Travel Enthusiast | Drone hobbyist