当前位置: > > > > TestMain – 没有要运行的测试
来源:stackoverflow
2024-04-23 10:18:37
0浏览
收藏
从现在开始,努力学习吧!本文《TestMain – 没有要运行的测试》主要讲解了等等相关知识点,我会在中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!
问题内容
我正在编写一个包,它编译 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) }
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
运行 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.
解决方案
很可能,您的问题与您传递给 os.mkdir(...)
的 mode 值有关。您提供的是 666
十进制,即 01232
八进制(或者如果您愿意,可以提供 d-w--wx-wt
的权限字符串),我认为这并不是您真正想要的。
您应该指定 0666
,而不是 666
– 前导 0 表示您的值采用八进制表示法。
此外,您的两个测试实际上是相同的;我建议使用 *t.run(...)
从单个顶级 test*
函数执行测试,而不是使用 testmain(...)
来执行设置。像这样的事情:
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
尝试运行 testmain()
就像尝试运行 main()
一样。你不这样做,操作系统会为你做这件事。
是在 go 1.4 中引入的,用于帮助设置/拆卸测试环境,并被调用而不是运行测试;引用发行说明:
#2
使用 ioutil.tempdir()
创建临时目录。
tmpdir, err := ioutil.tempdir("", "test_output") if err != nil { // handle err }
它将负责创建目录。您稍后应该使用 os.remove(tmpdir)
删除临时目录。
您可以将其与 中的建议的稍微修改版本一起使用,示例如下:
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) } }) } }
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持!更多关于Golang的相关知识,也可关注公众号。