title: TestMain not run
body: <p>I have a test package in go which tests some stuff that depend on reading a configuration. I want to read that configuration once before running all tests so I'm trying to use <code>TestMain(m *testing.M)</code>:</p>
  
  <p>main.go:</p>
  
  <pre><code>package tests
  
  import (
      ...
  )
  
  var logger = logging.MustGetLogger("tests")
  
  func TestMain(m *testing.M) {
      logger.Info("Initializing test suite")
  
      viper.SetConfigName("config")
      viper.AddConfigPath("..\\..\\")
      err := viper.ReadInConfig()
      if err == nil {
          os.Exit(m.Run())
      } else {
          logger.Fatal("Could not read configuration")
      }
  }
  </code></pre>
  
  <p>And I have another file in the same directory (and package) with a test.</p>
  
  <p>repository_test.go:</p>
  
  <pre><code>package tests
  
  import (
      ...
  )
  
  func TestCreation(t *testing.T) {
      aa := myModule.CreateRepository()
      assert.NotNil(t, aa)
  }
  </code></pre>
  
  <p>My problem is that the test fails because the configuration is not read from the file. When I try to debug the test in Gogland a breakpoint inside <code>TestMain</code> is not hit. When I run the tests from command line I don't see any printouts from <code>TestMain</code>.</p>
  
  <p>Is there something special I should do to make it work? From what I read online I understood that if I define <code>TestMain(m *testing.M)</code> then it's going to run just once for the package and that's where I'm supposed to write any setup or teardown code.</p>