gpt4 book ai didi

go - TestMain - 没有要运行的测试

转载 作者:IT王子 更新时间:2023-10-29 01:53:40 25 4
gpt4 key购买 nike

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

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

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

去测试输出:

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

运行 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.

最佳答案

#1

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

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

If the test code contains a function

func TestMain(m *testing.M) 

that function will be called instead of running the tests directly. The M struct contains methods to access and run the tests.


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

关于go - TestMain - 没有要运行的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54615873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com