TestMain - 没有要运行的测试

Sri*_*sam 3 testing go

我正在编写一个包,它编译 C 源文件并将输出写入另一个文件。我正在为这个包编写测试,我需要创建一个临时目录来写入输出文件。我正在使用TestMain函数来执行此操作。由于某种原因,当我刚刚运行测试时,我总是收到警告“没有要运行的测试” TestMain。我尝试调试该TestMain函数,可以看到临时目录确实已创建。当我手动创建testoutput目录时,所有测试都通过。

我正在从testdata目录加载两个 C 源文件,其中之一是故意错误的。

海湾合作委员会.go:

package gcc

import (
    "os/exec"
)

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

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)
}
Run Code Online (Sandbox Code Playgroud)

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 1
FAIL
FAIL    github.com/sriram-kailasam/relab/pkg/gcc        0.257s
Run Code Online (Sandbox Code Playgroud)

跑步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.
Run Code Online (Sandbox Code Playgroud)

ifn*_*tak 5

#1

尝试跑步TestMain()就像尝试跑步一样main()。你不这样做,操作系统会为你做这件事。

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

如果测试代码包含函数

func TestMain(m *testing.M) 
Run Code Online (Sandbox Code Playgroud)

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


#2

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

tmpDir, err := ioutil.TempDir("", "test_output")
if err != nil {
    // handle err
}
Run Code Online (Sandbox Code Playgroud)

它将负责创建目录。您应该稍后使用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)
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)