Unit Testing with Ginkgo : Part 1

Utkarsh Mani Tripathi
3 min readAug 9, 2017

Few days ago, when i was very new to OpenEBS, i didn’t understand much about it, and i wanted to contribute on this project. But i couldn’t find any comfortable starting point to get started with it. Then i was assigned to write the code that displays the OpenEBS Volume statsinto JSON format . Even though this was an easy task but It took me more than a week to understand the code flow and i completed this task successfully on time 😝. This was my first meaningful contribution on OpenEBS as well as on open source 😌. I thought writing unit test will be great idea to getting started with OpenEBS and i said to kiran mova that i want to write Unit Tests for the code i have written and asked “where should i start ?” . He suggested me to try Ginkgo.

I wrote two tests for the project OpenEBS and faced many challenges such as :

  • Understanding Ginkgo, this was very challenging because there are no such beginner friendly examples available.
  • Unfamiliarity with the REST calls and handling multiple requests.
  • Initializing the nested structures (It really looks so ugly).
  • Mocking of behaviours
  • And lot of silly errors due to improper decoding of Json data.

Ginkgo is really a very good tool to get started with unit testing. By the end of this blog you will become familiar with Ginkgo and learn how to use it for your project.

Prerequisites :
*
Go v1.4+installed on your machine
* Familiar with Golang

Installing Ginkgo : Ginkgo is simply amazing, you can use it for unit testing as well as to know how long a certain section of your code runs?
Just go get the following repos and you are ready to go Ginkgo.

go get github.com/onsi/ginkgo/ginkgo
go get github.com/onsi/gomega

Gomega is matcher library which will make unit testing great again 😜, its syntax is just like simple English. You will never feel that you are writing code. I will demonstrate unit testing using Ginkgo for the very basic function here and cover HTTP Unit Testing and mocking of behaviours later in the next blog.

This is the code that i have to test, now follow the steps given below

Steps :
1.
Go to the folder/package where your code is located (make sure it’s inside your gopath)
2. Run ginkgo bootstrap it will generate a test suite package_name_suite_test.gofor the package. It’s always good to have a separate test suite per package. This is required to run all the tests inside that package. You need not have to write anything in this file. Below is the auto generated code.

3. Run ginkgo generate filename in my case it is ginkgo generate sum . This will generate a file named filename_test.go. You have to write tests in this file. Have a look at the test code given below.

4. Run ginkgo or go test and it will show you the test result like this

Screenshot of the Output

Congrats ! your test passed successfully. This was the very quick demo of the testing framework Ginkgo. Check out their official docs here. Feel free to comment on any isues and stay tuned for part 2 .

That’s all folks ! As always thanks for reading. 😊

~ उत्कर्ष_उवाच

--

--