As a quick reminder, golang is a really fun programming language to use. It even includes testing out of the box! Unfortunately, this out-of-the-box testing framework isn’t all that great. It lacks the syntactic sugar of mature frameworks like rspec or gtest.
Of course, there are alternatives. I found an open-source library (licensed with Simplified BSD) called gocheck.
Gocheck, how do I love thee? Let me count the ways:
- Test fixtures
- Improved assertions
- Improved test output
- Sugar-coated syntax
- Test skipping
- Oh my
As usual, it’s time to guide you through a contrived example. Start by installing the package:
1
|
|
Let’s see, what should we make… how about a tip calculating library? We should start by testing, because we’re obsessed with TDD.
Test #1: Returns 0 for free meal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Not quite as obvious as the internal testing framework. First we hook up gocheck into the “go test” runner. Then we create a test suite; ours is empty for now and called MySuite
. We call Suite
to intialize the test runner with our custom suite. We then write our first test to assert that calculating the tip returns a value equal to 0. All tests must be prefixed with the word “Test”. Now I’ll write the implementation:
1 2 3 4 5 |
|
Running the tests…
1 2 3 4 |
|
Woohoo! All passed. What happens if we write a failing test?
1 2 3 4 |
|
Results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
A nasty failure that one. I’ll fix it and continue:
1 2 3 4 5 |
|
I want to create a Setup method for my entire suite. I’ll store some silly information there for information’s sake. The minBill and maxBill variables will only be set when I first load the suite.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
What if I wanted to set some information at the start of each test? I’ll log the current test number on the suite, updating it every time I run a test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Result:
1 2 3 4 5 6 |
|
You can create tear down methods for suites and tests in the same manner, replacing the appropriate words above.
There’s loads of other cool stuff gocheck can do, I’ve barely scratched the surface with what little experience I’ve had using it. Like any testing framework, I’m sure it has its advantages and disadvantages, but it sure beats the pants off the off-the-shelf framework Google includes with golang.