跳过CRAN测试,但在本地运行

Hel*_*123 5 r package cran

如果包由CRAN测试,有一种简单的方法可以跳过包中某些测试的执行?背景是,我喜欢有很多测试,总而言之,它们很耗时(对CRAN不好).

我知道有,testthat::skip_on_cran()但我不想使用包testthat来避免另一个依赖.我正在寻找一种模仿的el-cheapo方式testthat::skip_on_cran.

理想情况下,我希望在目录pkg/tests中有一个调用测试文件的测试文件(testfiles),并且如果我们正在使用它们则会产生干扰:

if (!on_cran) {
 ## these tests are run only locally/when not on CRAN
 # run test 1 (e.g. source a file with a test)
 # run test 2
}
# run test 3 - always
Run Code Online (Sandbox Code Playgroud)

Dir*_*tel 14

是! 您可以通过编程方式自动处理此问题.让我详细介绍一下我设置的两种方法:

隐式的通过版本号:这是由RCPP采取的办法很多年了,这是完全通用的,不依赖于任何其他程序包.我们的测试从文件开始tests/,然后移交给RUnit,但最后一部分是实现细节.

在主文件中tests/doRUnit.R我们这样做:

## force tests to be executed if in dev release which we define as
## having a sub-release, eg 0.9.15.5 is one whereas 0.9.16 is not
if (length(strsplit(packageDescription("Rcpp")$Version, "\\.")[[1]]) > 3) { 
    Sys.setenv("RunAllRcppTests"="yes")
}
Run Code Online (Sandbox Code Playgroud)

本质上,我们测试版本是否为abcd形式 - 如果是这样的结论,它是一个开发版本.这意味着"运行所有测试".而abc格式的发布版本将转到CRAN,而不会运行这些测试,因为它们会超出其时间限制.

在每个实际的单元测试文件中,我们可以决定是否要尊重变量并在设置时跳过测试,或者仍然执行:

.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"

if (.runThisTest) {

   ## code here that contains the tests

}
Run Code Online (Sandbox Code Playgroud)

此机制是完全自动的,不依赖于用户.(在实际的软件包版本中,还有另一个if ()测试包含在其中,它允许我们抑制测试,但这是我们在这里不需要的细节).

我仍然喜欢这种方法.

明确地通过资源文件 我们中的一些人工作的另一个包(最近很多)需要一个特定的后端可用.因此,在Rblpapi包中,我们测试了一个文件的存在,我的共同作者和我每个文件都在我们的$HOME目录下面,以便设置凭据和连接细节.如果文件丢失(例如在Travis CI,CRAN或其他用户上),则跳过测试.

我们选择将资源文件用作R文件; 如果找到options()它就会获取它,从而设置值.这样我们就可以直接控制是否启动测试.

## We need to source an extra parameter file to support a Bloomberg connection
## For live sessions, we use ~/.Rprofile but that file is not read by R CMD check
## The file basically just calls options() and sets options as needed for blpHost,
## blpPort, blpAutoConnect (to ensure blpConnect() is called on package load) and,
## as tested for below, blpUnitTests.
connectionParameterFile <- "~/.R/rblpapiOptions.R"
if (file.exists(connectionParameterFile)) source(connectionParameterFile)

## if an option is set, we run tests. otherwise we don't.
## recall that we DO need a working Bloomberg connection...
if (getOption("blpUnitTests", FALSE)) {

    ## ... more stuff here which sets things up

}
Run Code Online (Sandbox Code Playgroud)

与第一个用例类似,我们现在可以设置更多随后测试的变量.

显式地通过Travis CI 我们在rfoaas中使用的另一个选项是在Travis CI文件中设置管理它的环境变量:

env:
  global:
    - RunFOAASTests=yes
Run Code Online (Sandbox Code Playgroud)

其中,然后测试脚本回升:

## Use the Travis / GitHub integrations as we set this
## environment variable to "yes" in .travis.yml
##
## Set this variable manually if you want to run the tests
##
if (Sys.getenv("RunFOAASTests=yes") == "yes") runTests <- TRUE
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我还根据我的用户ID设置切换,因为我几乎是该项目的唯一贡献者:

## Also run the tests when building on Dirk's box, even whem
## the environment variable is not set
if (isTRUE(unname(Sys.info()["user"])=="edd")) runTests <- TRUE
Run Code Online (Sandbox Code Playgroud)

明确地通过另一个变量 你当然也可以依赖于你在所有包中使用的另一个变量.我发现这是个坏主意.如果你在shell中设置它,可以使用包A并将其设置为禁止测试但是然后切换到包B ---您可能会忘记取消设置变量然后无法测试.我最喜欢这种方法,不要使用它.