Testing 测试中的静态文件

Testing 测试中的静态文件,testing,go,Testing,Go,当我在Go中编写需要静态文件的测试时(例如一个文件hello.txt我在哪里测试我的程序hello.txt被正确读取),我应该将静态文件放在哪里?我应该如何在测试文件中处理它们 也就是说,当前我的设置是本地目录,GOPATH设置为此目录。我有 src/ mypkg/ myfile.go myfile_test.go testfiles/ hello.txt world.txt 现在在myfile\u test.go中

当我在Go中编写需要静态文件的测试时(例如一个文件
hello.txt
我在哪里测试我的程序
hello.txt
被正确读取),我应该将静态文件放在哪里?我应该如何在测试文件中处理它们

也就是说,当前我的设置是本地目录,
GOPATH
设置为此目录。我有

src/
   mypkg/
        myfile.go
        myfile_test.go
testfiles/
          hello.txt
          world.txt
现在在
myfile\u test.go
中,我不想使用绝对路径来引用
testfiles/hello.txt
。有什么惯用的方法吗


这是一个合理的布局吗

常见的方法是,例如

$GOPATH/src/
        mypkg/
                myfile.go
                myfile_test.go
                _testdata/
                        hello.txt
                        world.txt
然后,在foo_测试中,使用

f, err := os.Open("_testdata/hello.txt")
....


当测试二进制文件执行时,测试包确保CWD是
$GOPATH/src/mypkg

我了解的越多,我就越能看到投入了多少心思。
b, err := ioutil.ReadFile("_testdata/hello.txt")
....