Last active
August 29, 2015 14:16
-
-
Save jaredfolkins/ebf629c71ff2689d2fe3 to your computer and use it in GitHub Desktop.
TestMain() Pattern
This file contains hidden or 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
// 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