title: TestMain for all tests?
body: <p>I have a fairly large project with many integration tests sprinkled throughout different packages. I'm using build tags to separate unit, integration and e2e tests.</p>
  <p>I need to do some setup before running my integration and e2e tests, so I put a <code>TestMain</code> function in a <code>main_test.go</code> file in the root directory. It's pretty simple:</p>
  <pre class="lang-golang prettyprint-override"><code>//go:build integration || e2e
  // +build integration e2e
  
  package test
  
  import (
    ...
  )
  
  func TestMain(m *testing.M) {
    if err := setup(); err != nil {
      os.Exit(1)
    }
  
    exitCode := m.Run()
  
    if err := tearDown(); err != nil {
      os.Exit(1)
    }
  
    os.Exit(exitCode)
  
  }
  
  func setup() error {
    // setup stuff here...
    return nil
  }
  
  func tearDown() error {
    // tear down stuff here...
    return nil
  }
  </code></pre>
  <p>However, when I run test:</p>
  <pre class="lang-sh prettyprint-override"><code>$ go test -v --tags=integration ./...
  
  testing: warning: no tests to run
  PASS
  
  # all of my subdirectory tests now run and fail...
  </code></pre>
  <p>I really don't want to write a <code>TestMain</code> in each package that requires it and was hoping I could just get away with one in the root. Is there any solution that you could suggest? Thanks.</p>
  <p>The only alternative I can think of is setting up everything outside of code and then running the integration tests. Maybe some shell script that does setup and then calls <code>$ go test</code>?</p>