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.
Let’s examine the contents of the file.
LoanInfostruct which is a simple model with one field
interestInPercent. As the name suggests, its a model representing information about a Loan.
NewLoanInfoLoanInfo constructor function with basic validation.
Calculatorinterface which has one abstract method
SimpleInterest, takes principal amount (USD) and tenure (Years), returns a float64 value and an error.
SimpleCalculatorstruct which has one field of type LoanInfo and the concrete implementation of
*SimpleCalculatoras receiver. This enables java like
Is-Arelationship between Calculator and SimpleCalculator, read more here.
NewSimpleCalculatoris constructor function for initializing
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.
CalculatorTestSuiteis the struct which defines the test suite.
TestCalculatorTestSuiteis the function which runs the suite.
SetupTestis 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
Calculator interface type, Initialize it in
SetupTest. Here is the updated File.
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
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.
Let’s write the assertions now.
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.
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.