Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Created August 13, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwhatcott/03986dc2341c603db6c0 to your computer and use it in GitHub Desktop.
Save mdwhatcott/03986dc2341c603db6c0 to your computer and use it in GitHub Desktop.
golang testing package improvements
package something
import "testing"
type MyFixture struct {
*testing.T
sut Something // stuff under test
}
// Setup methods (optionally) defined on fixtures will be run before each
// Test method.
func (t *MyFixture) Setup() {
t.sut = NewSomething()
}
// Teardown methods (optionally) defined on fixtures will be run after each
// Test method.
func (t *MyFixture) Teardown() {
t.sut = nil // or whatever other cleanup is needed
}
// Each Test method is run on a separate instance of MyFixture, after the Setup
// and before Teardown methods.
func (t *MyFixture) TestDoSomething() {
t.Parallel()
if testing.Short() {
t.Skip("Long-running")
}
t.sut = t.sut.DoSomething()
t.checkResults()
}
// Here's another test method, run on it's own instance of MyFixture, after having run Setup and before Teardown.
func (t *MyFixture) TestDoSomethingElse() {
t.Parallel()
if testing.Short() {
t.Skip("Long-running")
}
t.sut = t.sut.DoSomethingElse()
t.checkResults()
}
func (t *MyFixture) checkResults() {
if t.sut == nil {
t.Error("It didn't work")
}
}
/* Sample output:
$ go test -v
=== RUN Test_MyFixture_Something
--- PASS: Test_MyFixture_Something (0.00s)
PASS
ok github.com/user/repo 0.005s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment