使用本地文件进行Golang测试 [英] Golang testing using local files

查看:272
本文介绍了使用本地文件进行Golang测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在使用本地文件对Golang进行测试时应该使用的最佳实践.

I'm looking for what best practice I should use when it comes to testing with Golang using local files.

通过使用本地文件,我的意思是为了测试功能,应用程序需要一些本地文件,因为该应用程序会频繁地从这些文件中读取数据.

By using local files, I mean that in order to test functionality, the application needs some local files, as the application reads from these files frequently.

我不确定是否应该在使用ioutil包tempdir和tempfile函数运行测试之前自己写临时文件,还是创建这样的测试文件夹;

I'm not sure if I should write temporary files myself just before running the tests using the ioutil package tempdir and tempfile functions, or create a test folder like so;

testing/...test_files_here
main.go
main_test.go

然后从里面的内容中读取

and then read from the contents inside

testing/...

谢谢

推荐答案

这是我当前的测试设置:

This is my current test setup:

app/
   main.go
   main_test.go
   main_testdata

   package1/
     package1.go 
     package1_test.go 
     package1_testdata1

   package2/
     package2.go 
     package2_test.go 
     package2_testdata1

特定于单个程序包的所有测试数据都放置在该程序包的目录中.多个程序包将使用的通用测试数据放置在应用程序根目录或$HOME中.

All the test data that is specific to a single package, is placed within the directory of that package. Common test data that will be used by multiple packages are either placed in the application root or in $HOME.

此设置对我有用.它很容易更改数据和测试,而无需进行额外的输入:

This set up works for me. Its easy to change the data and test, without having to do extra typing:

vim package1_test_data1; go test app/package1

这篇关于使用本地文件进行Golang测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆