Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jaredfolkins
Last active August 29, 2015 14:16
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 jaredfolkins/ebf629c71ff2689d2fe3 to your computer and use it in GitHub Desktop.
Save jaredfolkins/ebf629c71ff2689d2fe3 to your computer and use it in GitHub Desktop.
TestMain() Pattern
// Because TestMain() is an init method and should (in most cases) only be called once, one must wrap it to extend the functionality
//
// 1. This code should exist in a setup_test.go (descriptive name) file
// 2. Setup/Teardown methods along with variables/connections/assets for specific tests should be written inside of the specific test file
// Example Filename: some_random_test.go
// Setup Method: someRandomTestSetup()
// Teardown Method: someRandomTestTeardown()
// 3. The Setup/Teardown methods are added to the global testMainSetup()/testMainTeardown() to centralize and organize your test suite
package proj
import (
"log"
"os"
"testing"
)
func testMainSetup() {
log.Println("testMainSetup()")
someRandomTestSetup()
// do stuff ....
}
func testMainTeardown() {
log.Println("testMainTeardown()")
someRandomTestTeardown()
// do stuff ....
}
func TestMain(m *testing.M) {
log.Println("TestMain")
testMainSetup()
res := m.Run()
testMainTeardown()
os.Exit(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment