Introduction
The Go language provides an internal testing library, named testing, which is relatively slim due to the fact that the standard library correctness by itself is verified using it. The gocheck package, on the other hand, expects the standard library from Go to be working correctly, and builds on it to offer a richer testing framework for libraries and applications to use.
gocheck includes features such as:
- Helpful error reporting to aid on figuring problems out (see below)
- Richer test helpers: assertions which interrupt the test immediately, deep multi-type comparisons, string matching, etc
- Suite-based grouping of tests
- Fixtures: per suite and/or per test set up and tear down
- Benchmarks integrated in the suite logic (with fixtures, etc)
- Management of temporary directories
- Panic-catching logic, with proper error reporting
- Proper counting of successes, failures, panics, missed tests, skips, etc
- Explicit test skipping
- Support for expected failures
- Verbosity flag which disables output caching (helpful to debug hanging tests, for instance)
- Multi-line string reporting for more comprehensible failures
- Inclusion of comments surrounding checks on failure reports
- Fully tested (it manages to test itself reliably)
The following features will be available in future releases:
- Optional parallel test execution
Compatibility with "go test"
gocheck works as an extension to the testing package and to the "go test" runner. That means you can keep all the tests you currently have, and start using gocheck-based tests right away with no conflicts. The gocheck API was purposefully made similar to the testing module one to ease the migration as well.
Installing and updating
To start using gocheck right away, just run the following command:
go get launchpad.net/gocheck
To ensure you're using the latest version, run the following command instead:
go get -u launchpad.net/gocheck
If you get an error such as "bzr: no such file or directory", ensure you have the Bazaar DVCS installed (e.g. "sudo apt-get install bzr" in Ubuntu, or "sudo port install bzr" with MacPorts).
API documentation
Check it out online at:
Basic example
Here is a simple example of how to use gocheck.
package hello_test
import (
. "launchpad.net/gocheck"
"testing"
"os"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestHelloWorld(c *C) {
c.Check(42, Equals, "42")
c.Check(os.Errno(13), Matches, "perm.*accepted")
}
See Assertions and checks below for more information on these tests.
Using fixtures
To use fixtures, simply add one or more of the following methods to the test suite:
- func (s *SuiteType) SetUpSuite(c *C) - Run once when the suite starts running.
- func (s *SuiteType) SetUpTest(c *C) - Run before each test or benchmark starts running.
- func (s *SuiteType) TearDownTest(c *C) - Run after each test or benchmark runs.
- func (s *SuiteType) TearDownSuite(c *C) - Run once after all tests or benchmarks have finished running.
Here is an example preparing some data in a temporary directory before each test runs:
type Suite struct{
dir string
}
func (s *MySuite) SetUpTest(c *C) {
s.dir = c.MkDir()
// Use s.dir to prepare some data.
}
func (s *MySuite) TestWithDir(c *C) {
// Use the data in s.dir in the test.
}
Adding benchmarks
Benchmarks can be added by prefixing a method in the suite with "Benchmark". The method will be called with the usual *C argument, but unlike a normal test it is supposed to put the benchmarked logic within a loop iterating c.N times.
For example:
func (s *MySuite) BenchmarkLogic(c *C) {
for i := 0; i < c.N; i++ {
// Logic to benchmark
}
}
These methods are only run when in benchmark mode, using the -gocheck.b flag, and will present a result similar to the following when run:
PASS: myfile.go:67: MySuite.BenchmarkLogic 100000 14026 ns/op
PASS: myfile.go:73: MySuite.BenchmarkOtherLogic 100000 21133 ns/op
All the fixture methods are run as usual for a test method.
To obtain the timing for normal tests, use the -gocheck.v flag instead.
Skipping tests
Many times it's useful to skip one or multiple tests explicitly depending on factors such as the architecture being run or the availability of resources (network, etc). gocheck enables skipping individual tests using the Skip method inside the test itself, and skipping multiple tests by using Skip within SetUpSuite or SetUpTest.
As an example, here is a test suite which will skip all the tests within the suite unless the -live option is provided to go test.
var live = flag.Bool("live", false, "Include live tests")
type LiveSuite struct{}
func (s *LiveSuite) SetUpSuite(c *C) {
if *live {
c.Skip("-live not provided")
}
}
Running tests and output sample
To run the tests, simply use the go test tool as usual:
$ go test
----------------------------------------------------------------------
FAIL: hello_test.go:16: S.TestHelloWorld
hello_test.go:17:
c.Check(42, Equals, "42")
... obtained int = 42
... expected string = "42"
hello_test.go:18:
c.Check(os.Errno(13), Matches, "perm.*accepted")
... value os.Errno = 13 ("permission denied")
... regex string = "perm.*accepted"
OOPS: 0 passed, 1 FAILED
--- FAIL: hello_test.Test
FAIL
Assertions and checks
gocheck uses two methods of *C to verify expectations on values obtained in test cases: Assert and Check. Both of these methods accept the same arguments, and the only difference between them is that when Assert fails, the test is interrupted immediately, while Check will fail the test, return false, and allow it to continue.
Assert and Check have the following types:
func (c *C) Assert(obtained interface{}, checker Checker, ...args interface{})
func (c *C) Check(obtained interface{}, checker Checker, ...args interface{}) bool
Here are some usage examples:
func (s *S) TestSimpleChecks(c *C) {
c.Assert(value, Equals, 42)
c.Assert(s, Matches, "hel.*there")
c.Assert(err, IsNil)
c.Assert(foo, Equals, bar, Commentf("#CPUs == %d", runtime.NumCPU())
}
This last statement will display the provided message next to the usual debugging information in case the check fails.
By implementing the Checker interface, you can provide your own custom verifications. There are several standard checkers available. Please check the documentation for details and examples:
Selecting which tests to run
gocheck can filter tests out based on the test name or the suite name, or even both. To run tests selectively, simply pass the command line option -f when running go test. Note that this option is specific to gocheck, and won't affect go test itself.
Some examples:
$ go test -gocheck.f MyTestSuite
$ go test -gocheck.f "Test.*Works"
$ go test -gocheck.f "MyTestSuite.Test.*Works"
Verbose modes
gocheck offers two levels of verbosity modes through the -gocheck.v and -gocheck.vv flags. In the first mode, passing tests will also be reported. The second mode, though, will disable log caching entirely and will stream starting and ending suite calls and everything logged in between straight to the output. This is useful to debug hanging tests, for instance.
The -gocheck.v and -gocheck.vv flags were introduced in release r2010.12.26
License
gocheck is made available under the Simplified BSD License.