Created
August 13, 2015 17:29
-
-
Save mdwhatcott/03986dc2341c603db6c0 to your computer and use it in GitHub Desktop.
golang testing package improvements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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