我正在跟踪这个问题的提示,但我很不耐烦,我希望更快地运行我的测试,而不必等待R CMD check src在checking tests之前调用的30+检查。
我认为我可以做的是将一个--standalone选项添加到R-wiki页面中建议的doRUnit.R中,这样我就可以独立于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目录中的每个文件,我将如何做到这一点?
假设您有一个更好的测试模式(满足“快速”、“卸载”的需求),它是什么?
发布于 2009-12-15 12:44:05
您可能需要手动遍历R目录中的文件并对它们进行source(),可能需要使用类似于source(dir("/some/Path", pattern="*.R", full.names=TRUE)的内容。
但是我有种感觉,R CMD INSTALL做的更多。使用已安装的代码可能会更好。就像您所做的和wiki页面所建议的那样,直接运行单元测试已经相当不错了。所以我没有更好的计划。但要随时通知我们。
编辑:还请注意,R2.10.1为我们提供了加速R CMD INSTALL的新选择
2.10.1新特性 R安装有新的选项-不-R,--无数据,--无数据,-没有帮助,--没有-演示,这些文件用于特殊目的(例如,在不完全安装所有软件包的情况下建立帮助页数据库)。
这也会有帮助。
发布于 2009-12-15 13:07:36
对该脚本的进一步补充/更正。
我现在可以以doRUnit.R --standalone的形式调用它,也可以让R CMD check调用它。
if(!is.null(script.name)) {
setwd(dirname(script.name))
path <- '../inst/RUnit/'
}
.
.
.
if (is.null(opt$standalone)) {
cat("\nRunning unit tests of installed library\n")
library(package=pkg, character.only=TRUE)
} else {
cat("\nRunning unit tests of uninstalled library\n")
source(dir("../R/", pattern=".*\\.R", full.names=TRUE))
}https://stackoverflow.com/questions/1907129
复制相似问题