`go test`失败是因为它无法读取根目录中的文件。

huangapple go评论125阅读模式
英文:

`go test` fails because it cannot read files in root dir

问题

package model 导入了 package config,后者在其初始化过程中读取了 config.xml 文件。

当运行测试时,我得到了一个错误,它抱怨无法读取该文件。

我已经检查了文件是否存在,并且可以运行我的应用程序(读取配置文件)。

Golang 的测试是否不能在根目录下运行?有没有办法在不进行太多重构的情况下运行这个测试?

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) ➜

答案1

得分: 1

假设以下文件结构:

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))
	})
}

如果你在与 main.go 相同的文件夹中(在我的情况下是 /workspaces/gotests)运行 go test ./packages/read -run=TestReadFile -count=1 -v,你可以期望看到如下日志:

=== 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

答案2

得分: -1

你应该使用mock来模拟打开的文件以防止出现这个错误。使用现有的配置文件来测试你的应用程序是一个不好的主意。你可以创建一个根据文件名读取文件的单独函数,然后在测试中对其进行模拟。有很多情况可以模拟这个功能。

英文:

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.

huangapple
  • 本文由 发表于 2022年7月7日 03:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/72889134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定