我应该在哪里使用 testthat 将数据用于自动化测试? [英] Where should I put data for automated tests with testthat?

查看:15
本文介绍了我应该在哪里使用 testthat 将数据用于自动化测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Hadley 的基于 testthat 的方法 对我的包进行自动化测试.

I am using Hadley's testthat-based approach for automated testing of my package.

使用这种方法,最适合放置测试数据文件的位置是什么?这些文件仅由 tests/testthat 中的测试脚本使用,而不是 R/中的任何其他函数?

With this approach, what is the most suitable place to put test data files that is files only used by the test scripts in tests/testthat), but not by any other functions in R/?

我目前的方法是将它们放在 tests/testdata 中,然后使用相对路径而不是 system.file 从那里 read.table (为了避免需要安装包来运行测试).

My current approach is to put them in tests/testdata, and then read.table from there with a relative path rather than with system.file (in order to avoid the need to install the package to run tests).

有标准的方法吗?

推荐答案

借鉴 Ben Bolker 的评论:

Lifting from Ben Bolker's comments:

我使用 inst/testdata 然后system.file("testdata",...,package="my_package")

I use inst/testdata and then system.file("testdata",...,package="my_package")

这种方法的优点:

  • 您可以保持文件结构整洁,尤其是当您有许多数据文件和/或测试时.
  • inst 中的文件已安装这一事实是长期的规范 R 实践;system.file("testdata", "some_file") 似乎总是比 ../testdata/some_file 更安全.我在进行 R CMD 检查 时使用相对文件路径时遇到了不好的经历.
  • 与 Sathish 的回答不同,它不依赖于将您的数据存储"为 R 代码.
  • You can keep your file structure neat, especially if you have many data files and/or tests.
  • The fact that files in inst are installed is long-standing canonical R practice; it seems safer that system.file("testdata", "some_file") will always work than that ../testdata/some_file will do. I've had bad experiences using relative file paths when doing R CMD check.
  • Unlike Sathish's answer, it doesn't depend on your data being "stored" as R code.

这篇关于我应该在哪里使用 testthat 将数据用于自动化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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