General global test utilities.
Test helpers like test.WithTestDatabase
and test.WithTestServer
require usage of a closure, as these functions automatically manage the setup and teardown (e.g. server shutdown, db connection drop) for your testcase.
Other pkgs don't have this requirement (e.g. the initialization code for test.NewTestMailer
which covers the setup for the mailer
mock), thus, please use this With*
convention incl. closure only when it makes sense.
This are your global db test fixtures, that are only available while testing. However, feel free to setup specialized fixtures per package if required (e.g. just initialize an additional IntegreSQL template).
Please use this convention to specify test only utility functions.
Golden files can be created by using the Snapshoter.Save(t TestingT, data ...interface{})
method. The snapshot can be configured to force an update, use a different replacer function or to set a different file location and suffix for the snaphot.
A snapshot can be updated by either calling the Update(true)
method, or by using the global override by setting the environment variable TEST_UPDATE_GOLDEN
to true
. To update all snapshots the call might look like this: TEST_UPDATE_GOLDEN=true make test
.
Note that Go will ignore directories or files that begin with "." or "_", so you have more flexibility in terms of how you name your test data directory.
Go build ignores directory named testdata. The Go tool will ignore any directory in your $GOPATH that starts with a period, an underscore, or matches the word testdata When go test runs, it sets current directory as package directory
- https://github.com/golang-standards/project-layout/blob/master/test/README.md
- https://medium.com/@povilasve/go-advanced-tips-tricks-a872503ac859
- https://dave.cheney.net/2016/05/10/test-fixtures-in-go
Examples:
- https://github.com/openshift/origin/tree/master/test (test data is in the
/testdata
subdirectory)
https://github.com/golang-standards/project-layout/tree/master/test