mercredi 22 février 2017

golang TestMain() function sets variable that can't be accessed by tests

I've got the following TestMain function:

func TestMain(m *testing.M) {
  db := "[working_db_connection]"
  dbInstance, _ := InitializeRepo(db, 2)
  runTests := m.Run()
  os.Exit(runTests)
}

and the following sample test

func TestSomeFeature(t *testing.T) {
  fmt.Println(dbInstance)
}

The function TestSomeFeature does run, but says dbInstance is undefined. Why would this not have access to the variable? From examples I'm seeing variables et in TestMain are accessed with this syntax.

Aucun commentaire:

Enregistrer un commentaire