Coming from a ruby background and having done a lot of TDD using the sweet RSpec framework prompted me to look out for the testing frameworks available in Go.
Go comes with a default testing library called “testing”.
First of all, we need to create
a _test.go file in the same folder as the file you want to test. Both the files should also be in the same package. Inside the
_test.go file, any function-name starting with
Test will be tested. Such functions are expected to take a pointer to
Here we use expected and actual values to evaluate the condition.
To run the test use the command
go test in the folder where this file resides, you should see that all tests are running. On running the test we get a PASS message.
Test written in this way lacked the verbosity of typical Rspec Unit Test. To improve the situation there exist options like "Testify".
It is a assertion based testing framework. It includes packeges for:
- Easy assertions
- HTTP response trapping
- Testing suite interfaces and functions
The above spec demonstrate an example of assertion based test using assert package in testify.
There are other frameworks available too like "Ginkgo" which is a BDD testing framework with functionalities very similar to Rspec.