执行 Go 文件时出错:文件不存在
来源:stackoverflow
时间:2024-03-16 22:24:30 453浏览 收藏
在执行 Go 测试时,指定测试文件可能会导致文件不存在的错误。这是因为 Go 测试框架默认在当前目录下运行所有测试,无需指定测试文件。为了解决此问题,应使用 `go test -run` 命令指定要运行的特定测试,而不是文件。此命令允许指定顶级测试名称或子测试匹配模式,从而更精确地控制要执行的测试。
我有来自此 google github 存储库的两个 go 文件:
- certificate_tag.go
- certificate_tag_test.go
我已经克隆了存储库,因此所有相关文件所在的目录结构完全相同:
现在我一一运行此文件中的以下命令来执行它:
go build -o c:/tmp/certificate_tag common/certificate_tag/certificate_tag.go
这会在 c:/tmp/certificate_tag 处创建一个文件,如下所示:
但是现在当我运行下面的命令时会导致错误:
go test common/certificate_tag/certificate_tag_test.go common/certificate_tag/certificate_tag.go -tag-binary-dir "c:/tmp"
错误文字如下:
$ 去测试 common/certificate_tag/certificate_tag_test.go common/certificate_tag/certificate_tag.go -tag-binary-dir "c:/tmp" --- 失败:testprintappishedtag (0.00s) certificate_tag_test.go:74: 执行“c:\tmp\certificate_tag”时出错:exec:“c:\tmp\certificate_tag”: 文件 不存在;输出: --- 失败:testsetsuperfluouscerttag (0.01s) certificate_tag_test.go:101: 测试输入 testdata\chromesetup.exe,执行“c:\tmp\certificate_tag”时出错:exec: “c:\tmp\certificate_tag”: 文件不存在;输出:失败失败 命令行参数 4.640s 失败
testprintappishedtag 的代码如下:
func testprintappendedtag(t *testing.t) {
cmd := exec.command(tagbinary, "--dump-appended-tag", sourceexe)
output, err := cmd.combinedoutput()
if err != nil {
t.fatalf("error executing %q: %v; output:\n%s", tagbinary, err, output)
}
if out := string(output); !strings.contains(out, existingtagsubstring) {
t.errorf("output of --dump-appended-tag didn't contain %s, as expected. got:\n%s", existingtagsubstring, out)
}
}
为什么无法找到上面步骤 1 中在 c:/tmp/certificate_tag 创建的文件,即使它在那里?
更新:根据 volker 和 chen 的建议,现在我正在运行以下命令:
go test -run TestPrintAppendedTag
它给出以下错误:
certificate_tag_test.go:75: 执行“\tmp\certificate_tag”时出错: exec: "\tmp\certificate_tag": 文件不存在;
正确答案
运行go测试时,不需要指定测试的.go文件,只需运行go test -run即可运行当前目录下的所有测试。如果您想要更具体的内容,则需要指定测试名称,而不是文件。
go test # Run all tests. go test -run Foo # Run top-level tests matching "Foo", such as "TestFooBar". go test -run Foo/A= # For top-level tests matching "Foo", run subtests matching "A=". go test -run /A=1 # For all top-level tests, run subtests matching "A=1".
查看 go 博客中的 testing 软件包文档和其他 examples。
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《执行 Go 文件时出错:文件不存在》文章吧,也可关注golang学习网公众号了解相关技术文章。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
Golang · Go问答 | 1天前 | 并发 · channel · Mutex · 性能优化 · Go问答 · benchmark · Goroutine channel Mutex 并发性能 锁等待 benchmark Go问答268 收藏
-
Golang · Go问答 | 1天前 | 并发 · map · RWMutex · sync.Map · Go问答 · 数据竞争 RWMutex sync.Map map并发读写 Go问答 concurrent map read and map write291 收藏
-
Golang · Go问答 | 1天前 | 并发 · channel · range · Close · Go问答 · 并发退出 Channel关闭 Go问答 close channel range退出 发送方关闭427 收藏
-
Golang · Go问答 | 1天前 | goroutine · HTTP · Context · 超时控制 · Go问答 · WithTimeout Go问答 context取消 QueryContext HTTP请求取消 goroutine退出120 收藏
-
Golang · Go问答 | 1天前 | goroutine · pprof · Go问答 · 线上排查 · 性能调优 · pprof goroutine泄漏 服务告警 Go问答 context取消 Go线上排查374 收藏
-
Golang · Go问答 | 1天前 | goroutine · Context · 超时控制 · Go问答 · 协程泄漏 · Go Goroutine 超时控制 context.WithTimeout 取消信号 协程泄漏 ctx.Done147 收藏
-
Golang · Go问答 | 1天前 | 闭包 · go · Go问答 · 循环变量 · Go1.22 · 代码迁移 · 闭包 循环变量 go vet Go问答 Go 1.22 for range loopclosure356 收藏
-
127 收藏
-
449 收藏
-
Golang · Go问答 | 2天前 | sync.Pool · reset · 对象复用 · Go问答 · 线上排查 · Go sync.Pool Go问答 sync.Pool串数据 Reset清理 对象池复用342 收藏
-
Golang · Go问答 | 2天前 | 标准库 · 文件读取 · io.Reader · Go问答 · io.EOF · Go read io.Reader io.EOF Go问答 n err 读取循环486 收藏
-
181 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习