unit-testing - RUnit测试卸载包

标签 unit-testing r

我正在按照提示前往 this question ,但我很不耐烦,我想更快地运行我的测试,而不必等待 R CMD check srcchecking tests 之前调用的 30 多个检查。

我认为我可以做的是向 that R-wiki page 中建议的 doRUnit.R 添加一个 --standalone 选项。这样我就可以独立于 R CMD 运行单元测试。

我在脚本中添加了这些行:

  opt <- list(standalone=NULL)
  if(require("getopt", quietly=TRUE)) {
    ## path to unit tests may be given on command line, in which case
    ## we also want to move the cwd to this script
    opt <- getopt(matrix(c('standalone', 's', 0, "logical"),
                         ncol=4, byrow=TRUE))
    if(!is.null(opt$standalone)) {
      ## switch the cwd to the dir of this script
      args <- commandArgs()
      script.name <- substring(args[substring(args, 1, 7)=="--file="], 8, 1000)
      if(!is.null(script.name))
        setwd(dirname(script.name))
    }
  }

通过此更改,脚本会找到 test.*\.R 文件,独立于我调用脚本的目录。

现在剩下的问题是doRUnit.R 脚本加载已安装 库,它不会source() 组成文件图书馆。

假设我想加载 R 目录中的每个文件,我该怎么做?

假设你有一个更好的测试模式(满足“快速”、“未安装”的要求),它是什么?

最佳答案

您可能需要手动遍历 R 目录中的文件和 source() 它们,可能使用类似 source(dir("/some/Path", pattern="*.R", full.names=TRUE).

但我觉得 R CMD INSTALL 做得更多一些。从安装的代码开始工作可能会更好。直接运行你的单元测试,就像你所做的那样,正如 wiki 页面所建议的那样,已经很不错了。所以我没有更好的方案。但请随时通知我们。

编辑:另请注意,R 2.10.1 为我们提供了加速 R CMD INSTALL 的新选项:

2.10.1 NEW FEATURES

R CMD INSTALL has new options --no-R, --no-libs, --no-data, --no-help, --no-demo, --no-exec, and --no-inst to suppress installation of the specified part of the package. These are intended for special purposes (e.g. building a database of help pages without fully installing all packages).

这也应该有所帮助。

关于unit-testing - RUnit测试卸载包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1907129/

相关文章:

unit-testing - Mocks 是如何使用的?

ruby-on-rails - 我应该使用 def 或 test 关键字编写 Rails 测试吗?

r - 更改字体类型并在 corrplot 相关图的标签中添加下标或希腊字母

r - 将基本图形从 R Studio 复制粘贴到 Word 文档的简便方法

r - 绘制两条曲线之间的阴影区域

r - 保持柱水恒定

javascript - 在对显示模块进行单元测试时如何 stub 私有(private)函数

python - 如何从 python 脚本运行 python 文件中的所有测试?

r - 在 r 中进行非线性最小二乘拟合

java - GAE (Java) 高复制数据存储测试 - 测试用例之间未清除数据存储