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.

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

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

We will use the package and style in this lab.

Lets examine the Anatomy of the above test file.

  • is the struct which defines the test suite.
  • is the function which runs the suite.
  • 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 method called . We need SimpleCalculator Initialized for the Test. We can create a new variable in of interface type, Initialize it in . Here is the updated File.

testing SimpleInterest method

In test function, i am using assertion method to verify what is being returned vs what is expected. thats it, now we have written a simple test for 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 to run each test in its own test context. We can use the 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