TestMain - 没有要运行的测试
来源:stackoverflow
时间:2024-04-23 10:18:37 238浏览 收藏
从现在开始,努力学习吧!本文《TestMain - 没有要运行的测试》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!
我正在编写一个包,它编译 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()
一样。你不这样做,操作系统会为你做这件事。
TestMain 是在 go 1.4 中引入的,用于帮助设置/拆卸测试环境,并被调用而不是运行测试;引用发行说明:
#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) } }) } }
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习