2009-12-15 122 views
3

我正在关注this question的提示,但我很不耐烦,希望能够更快地运行我的测试,而不必等待之前R CMD check src所调用的30+个检查。RUnit测试已卸载的软件包

什么,我想我唯一能做的就是增加一个--standalone选项将doRUnit.Rthat R-wiki page建议,这样我可以独立运行的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目录中的每个文件,我该怎么做?

假设您有一个更好的测试模式(满足“快速”,“卸载”的要求),它是什么?

回答

3

您可能需要手动循环遍历R目录中的文件和source()这些文件,可能是source(dir("/some/Path", pattern="*.R", full.names=TRUE)之类的文件。

但我觉得R CMD INSTALL做的更多一点。从安装的代码开始工作可能会更好。直接运行你的单元测试,就像你和维基页面所建议的一样,已经很不错了。所以从我这里没有更好的计划。但保持我们的发布。

编辑:另外要注意的是,R 2.10.1为我们提供了新的选择,以加速R CMD INSTALL

2.10.1新功能

[R CMD INSTALL有新的选项--no-R, -no-libs,--no-data,--no-help,--no-demo,--no-exec和--no-inst禁止安装指定的 软件包的一部分。这些是用于特殊目的的 (例如, 构建帮助页面的数据库 而不完全安装所有 包)。

这也应该有帮助。

+0

到底调用,我混我自己的答案与你的:我第一次安装包,然后我运行doRUnit - -standalone。它更快,我看到控制台上的输出。 – mariotomo 2009-12-18 14:16:24

+0

以后更新:我有一个[Makefile](https://r-forge.r-project.org/scm/viewvc.php/\*checkout\*/pkg/inst/RUnit/Makefile?root = delftfews)在unittests目录中定义一个目标(xtest),它产生在emacs的'* compilation *'缓冲区中有用的输出。 – mariotomo 2010-08-09 09:11:17

0

对脚本的进一步增加/更正。

我现在可以调用它作为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)) 
    }