title: TestMain - no tests to run
body: <p>I am writing a package which compiles a C source file and writes the output to another file. I am writing tests for this package, and I need to create a temporary directory to write the output files. I am using <code>TestMain</code> function to do this. For some reason, I always get the warning "no tests to run" when I just run the <code>TestMain</code> test. I tried to debug the <code>TestMain</code> function and I can see that the temporary directory is indeed created. When I create the <code>testoutput</code> directory manually, all tests pass.</p>
  
  <p>I am loading two C source files from <code>testdata</code> directory, one of which is intentionally wrong.</p>
  
  <p>gcc.go:</p>
  
  <pre><code>package gcc
  
  import (
      "os/exec"
  )
  
  func Compile(inPath, outPath string) error {
      cmd := exec.Command("gcc", inPath, "-o", outPath)
      return cmd.Run()
  }
  </code></pre>
  
  <p>gcc_test.go:</p>
  
  <pre><code>package gcc
  
  import (
      "os"
      "path/filepath"
      "testing"
  )
  
  func TestOuputFileCreated(t *testing.T) {
      var inFile = filepath.Join("testdata", "correct.c")
      var outFile = filepath.Join("testoutput", "correct_out")
  
      if err := Compile(inFile, outFile); err != nil {
          t.Errorf("Expected err to be nil, got %s", err.Error())
      }
  
      if _, err := os.Stat(outFile); os.IsNotExist(err) {
          t.Error("Expected output file to be created")
      }
  }
  
  func TestOuputFileNotCreatedForIncorrectSource(t *testing.T) {
      var inFile = filepath.Join("testdata", "wrong.c")
      var outFile = filepath.Join("testoutput", "wrong_out")
  
      if err := Compile(inFile, outFile); err == nil {
          t.Errorf("Expected err to be nil, got %v", err)
      }
  
      if _, err := os.Stat(outFile); os.IsExist(err) {
          t.Error("Expected output file to not be created")
      }
  }
  
  func TestMain(m *testing.M) {
      os.Mkdir("testoutput", 666)
      code := m.Run()
      os.RemoveAll("testoutput")
      os.Exit(code)
  }
  </code></pre>
  
  <p><code>go test</code> output:</p>
  
  <pre><code>sriram@sriram-Vostro-15:~/go/src/github.com/sriram-kailasam/relab/pkg/gcc$ go test
  --- FAIL: TestOuputFileCreated (0.22s)
          gcc_test.go:14: Expected err to be nil, got exit status 1
  FAIL
  FAIL    github.com/sriram-kailasam/relab/pkg/gcc        0.257s
  </code></pre>
  
  <p>Running <code>TestMain</code>:</p>
  
  <pre><code>Running tool: /usr/bin/go test -timeout 30s github.com/sriram-kailasam/relab/pkg/gcc -run ^(TestMain)$
  
  ok      github.com/sriram-kailasam/relab/pkg/gcc    0.001s [no tests to run]
  Success: Tests passed.
  </code></pre>