`go test`失败了,因为它无法读取root dir中的文件

发布于 2025-02-13 09:20:23 字数 509 浏览 0 评论 0原文

初始化中读取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2025-02-20 09:20:24

假设以下文件结构:

workspaces
└── gotests
    └── main.go
        packages
        └── read
            └── file_test.go
        testfile
        └── test.txt

file_test.go内容:

package read_test

import (
    "fmt"
    "os"
    "testing"
)

func TestReadFile(t *testing.T) {
    t.Run("I read a file", func(t *testing.T) {
        currentDir, err := os.Getwd()
        if err != nil {
            panic(err)
        }

        executable, err := os.Executable()
        if err != nil {
            panic(err)
        }

        fmt.Printf("Current dir: %v\n", currentDir)
        fmt.Printf("Executable: %v\n", executable)

        bytes, err := os.ReadFile("../../testfile/test.txt")
        if err != nil {
            panic(err)
        }

        fmt.Println(string(bytes))
    })
}

如果您运行GO test ./packages/read -run = testReadFile -count -count = 1 -V在同一文件夹中main.go(在我的情况下,/workspaces/gotests),您可以期待以下日志:

=== RUN   TestReadFile
=== RUN   TestReadFile/I_read_a_file
Current dir: /workspaces/gotests/packages/read
Executable: /tmp/go-build1080340781/b001/read.test
Im some test text.
--- PASS: TestReadFile (0.00s)
    --- PASS: TestReadFile/I_read_a_file (0.00s)
PASS

Assume the following file structure:

workspaces
└── gotests
    └── main.go
        packages
        └── read
            └── file_test.go
        testfile
        └── test.txt

file_test.go contents:

package read_test

import (
    "fmt"
    "os"
    "testing"
)

func TestReadFile(t *testing.T) {
    t.Run("I read a file", func(t *testing.T) {
        currentDir, err := os.Getwd()
        if err != nil {
            panic(err)
        }

        executable, err := os.Executable()
        if err != nil {
            panic(err)
        }

        fmt.Printf("Current dir: %v\n", currentDir)
        fmt.Printf("Executable: %v\n", executable)

        bytes, err := os.ReadFile("../../testfile/test.txt")
        if err != nil {
            panic(err)
        }

        fmt.Println(string(bytes))
    })
}

If you run go test ./packages/read -run=TestReadFile -count=1 -v in the same folder as main.go (in my case, /workspaces/gotests), you can expect a log like:

=== RUN   TestReadFile
=== RUN   TestReadFile/I_read_a_file
Current dir: /workspaces/gotests/packages/read
Executable: /tmp/go-build1080340781/b001/read.test
Im some test text.
--- PASS: TestReadFile (0.00s)
    --- PASS: TestReadFile/I_read_a_file (0.00s)
PASS
一身软味 2025-02-20 09:20:24

您应该模拟打开文件以防止此错误。使用现有配置文件测试您的应用是不好的。您可以创建一个单独的功能,以通过名称读取文件,然后在测试中模拟此文件。有很多情况可以模拟此功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文