dqh19413 2019-02-10 11:24
浏览 75
已采纳

TestMain-没有要运行的测试

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 TestMain function to do this. For some reason, I always get the warning "no tests to run" when I just run the TestMain test. I tried to debug the TestMain function and I can see that the temporary directory is indeed created. When I create the testoutput directory manually, all tests pass.

I am loading two C source files from testdata directory, one of which is intentionally wrong.

gcc.go:

package gcc

import (
    "os/exec"
)

func Compile(inPath, outPath string) error {
    cmd := exec.Command("gcc", inPath, "-o", outPath)
    return cmd.Run()
}

gcc_test.go:

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)
}

go test output:

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

Running TestMain:

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.
  • 写回答

2条回答 默认 最新

  • dpsyssiv90846 2019-02-10 19:13
    关注

    Odds are, your problems are around the mode value you're passing to os.Mkdir(...). You're providing 666 decimal, which is 01232 octal (or if you prefer, a permissions string of d-w--wx-wT) which I assume is not really what you're looking for.

    Instead of 666, you should specify 0666 -- the leading 0 indicates your value is in octal notation.


    Also, your two tests are virtually identical; instead of using a TestMain(...) to perform your setup, I'd suggest using *T.Run(...) to execute your tests from a single, top-level Test* func. Something like this:

    gcc_test.go:

    package gcc
    
    import (
      "testing"
      "path/filepath"
      "os"
    )
    
    const testoutput = "testoutput"
    
    type testcase struct {
      inFile  string
      outFile string
      err     error
    }
    
    func (tc *testcase) test(t *testing.T) {
      var (
        in  = filepath.Join("testdata", tc.inFile)
        out = filepath.Join(testoutput, tc.outFile)
      )
    
      if err := Compile(in, out); err != tc.err {
        t.Errorf("Compile(%q, %q) == %v; Wanted %v", in, out, err, tc.err)
      }
    }
    
    func TestCompile(t *testing.T) {
      os.Mkdir(testoutput, 0666)
    
      tests := map[string]*testcase{
        "correct": &testcase{"correct.c", "correct_out", nil},
        "wrong":   &testcase{"wrong.c", "wrong_out", expectedError},
      }
    
      for name, tc := range tests {
        t.Run(name, tc.test)
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败