为了账号安全,请及时绑定邮箱和手机立即绑定

TestMain - 没有要运行的测试

TestMain - 没有要运行的测试

Go
月关宝盒 2023-06-05 18:17:57
我正在编写一个编译 C 源文件并将输出写入另一个文件的包。我正在为这个包编写测试,我需要创建一个临时目录来写入输出文件。我正在使用TestMain函数来做到这一点。出于某种原因,当我刚刚运行测试时,我总是收到“没有要运行的测试”的警告TestMain。我尝试调试该TestMain函数,我可以看到确实创建了临时目录。当我手动创建testoutput目录时,所有测试都通过了。我正在从目录加载两个 C 源文件testdata,其中一个是故意错误的。gcc.go:package gccimport (    "os/exec")func Compile(inPath, outPath string) error {    cmd := exec.Command("gcc", inPath, "-o", outPath)    return cmd.Run()}gcc_test.go:package gccimport (    "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输出: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 1FAILFAIL    github.com/sriram-kailasam/relab/pkg/gcc        0.257s运行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 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

#1

尝试运行TestMain()就像尝试运行main()。你不这样做,操作系统为你做这件事。

TestMain是在 Go 1.4 中引入的,用于帮助设置/拆卸测试环境,它被调用而不是运行测试;引用发行说明:

如果测试代码包含函数

func TestMain(m *testing.M)

该函数将被调用而不是直接运行测试。M 结构包含访问和运行测试的方法。


#2

用于ioutil.TempDir()创建临时目录。

tmpDir, err := ioutil.TempDir("", "test_output")

if err != nil {

    // handle err

}

它将负责创建目录。您稍后应该使用os.Remove(tmpDir)删除临时目录。


您可以将它与Tim Peoples的建议稍微修改后的版本一起使用,例如:


func TestCompile(t *testing.T) {

    tmpDir, err := ioutil.TempDir("", "testdata")

    if err != nil {

        t.Error(err)

    }

    defer os.Remove(tmpDir)


    tests := []struct {

        name, inFile, outFile string

        err                   error

    }{

        {"OutputFileCreated", "correct.c", "correct_out", nil},

        {"OutputFileNotCreatedForIncorrectSource", "wrong.c", "wrong_out", someErr},

    }


    for _, test := range tests {

        var (

            in  = filepath.Join("testdata", test.inFile)

            out = filepath.Join(tmpDir, test.outFile)

        )


        t.Run(test.name, func(t *testing.T) {

            err = Compile(in, out)

            if err != test.err {

                t.Errorf("Compile(%q, %q) == %v; Wanted %v", in, out, err, test.err)

            }

        })

    }

}


查看完整回答
反对 回复 2023-06-05
?
米脂

TA贡献1836条经验 获得超3个赞

很可能,您的问题与您传递给的模式os.Mkdir(...)值有关。您提供的是666十进制,它是01232八进制的(或者,如果您愿意,权限字符串为d-w--wx-wT),我认为这并不是您真正想要的。


而不是666,您应该指定0666-- 前导 0 表示您的值是八进制表示法。


另外,您的两个测试实际上是相同的;与其使用 aTestMain(...)来执行设置,我建议使用*T.Run(...)从单个顶级Test*函数执行测试。是这样的:


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)

  }

}


查看完整回答
反对 回复 2023-06-05
  • 2 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信