`go test`失败了,因为它无法读取root dir中的文件
初始化中读取config.xml
软件包模型
是导入package config
,在运行测试时的 ;我遇到一个错误,抱怨它无法读取文件。
我已经检查了该文件是否存在,并且可以运行我的应用程序(读取配置文件)
Golang测试是否在rootdir中运行吗?有没有办法在没有太多重构的情况下进行此测试?
server git:(main) ➜ go test -v ./model/...
2022/07/06 15:46:21 config.go:72: Reading config file config.yaml
2022/07/06 15:46:21 config.go:75: open config.yaml: no such file or directory
FAIL github.com/texbazaar/server/model 0.141s
FAIL
server git:(main) ➜
package model
is imports package config
which reads config.xml
in it's init
When running test; I get an error complaining it cannot read the file.
I've checked the file does exists and I can run my application(read the config file)
Does golang test not run in rootdir ? Is there a way I can run this test without too much refactoring?
server git:(main) ➜ go test -v ./model/...
2022/07/06 15:46:21 config.go:72: Reading config file config.yaml
2022/07/06 15:46:21 config.go:75: open config.yaml: no such file or directory
FAIL github.com/texbazaar/server/model 0.141s
FAIL
server git:(main) ➜
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设以下文件结构:
file_test.go
内容:如果您运行
GO test ./packages/read -run = testReadFile -count -count = 1 -V
在同一文件夹中main.go
(在我的情况下,/workspaces/gotests
),您可以期待以下日志:Assume the following file structure:
file_test.go
contents:If you run
go test ./packages/read -run=TestReadFile -count=1 -v
in the same folder asmain.go
(in my case,/workspaces/gotests
), you can expect a log like:您应该
模拟
打开文件以防止此错误。使用现有配置文件测试您的应用是不好的。您可以创建一个单独的功能,以通过名称读取文件,然后在测试中模拟此文件。有很多情况可以模拟此功能。You should
mock
opened files to prevent this error. It's bad idea to test your app with existing configuration files. You can create a separate function for reading the file by name and then mock this in your tests. There are a lot of cases to mock this functionality.