Thursday, June 9, 2016

Testing in Golang

Golang is one of the programming language which use convention over configuration. In Go application, we can write a test file by adding Test suffix to test source file. Then this file will be ignored in Go build process. And also test runner can inspect test functions in these files and execute tests.
There are three types of tests in Go.

  • Basic Tests (Regular tests)
Basic tests are for identify application is performing accurately. These tests required to have Test prefix in test functions.
  • Benchmark Tests

Benchmark test are for identify performance with related to limits. These tests required to have Benchmark prefix in functions.

  • Example Tests
Example tests are integrated with GoDoc tool. These test required to have Example as prefix.
In this post, we will create a Basic test. We are going to do Test Driven Development, therefore we need to first write test functions.
Then write the actual code.
Now using following command we can run test runner. The output will be like below.
Next will change result comparing value for demonstration purpose. Then our test function become incorrect.
Run go test command again. Then output is like below.

No comments:

Post a Comment