Skip to content

Instantly share code, notes, and snippets.

@mvmaasakkers
Last active November 8, 2016 06:40
Show Gist options
  • Save mvmaasakkers/5cff09462f9fdb7bca43b0572b27809a to your computer and use it in GitHub Desktop.
Save mvmaasakkers/5cff09462f9fdb7bca43b0572b27809a to your computer and use it in GitHub Desktop.
init_test.go TestMain
// This file is part of https://github.com/mvmaasakkers/gohttptestmongodb/
// Server holds the dbtest DBServer
var Server dbtest.DBServer
// TestMain wraps all tests with the needed initialized mock DB and fixtures
func TestMain(m *testing.M) {
// The tempdir is created so MongoDB has a location to store its files.
// Contents are wiped once the server stops
tempDir, _ := ioutil.TempDir("", "testing")
Server.SetPath(tempDir)
// My main session var is now set to the temporary MongoDB instance
Session = Server.Session()
// Make sure to insert my fixtures
insertFixtures()
// Run the test suite
retCode := m.Run()
// Make sure we DropDatabase so we make absolutely sure nothing is left or locked while wiping the data and
// close session
Session.DB(MongoDBDatabase).DropDatabase()
Session.Close()
// Stop shuts down the temporary server and removes data on disk.
Server.Stop()
// call with result of m.Run()
os.Exit(retCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment