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

Skip tests on CRAN, but run locally

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

我知道有 testthat::skip_on_cran() 但我不想使用包 testthat 以避免另一个依赖。我正在寻找一种模仿 testthat::skip_on_cran.

的廉价方法

理想情况下,我希望在目录 pkg/tests 中有一个测试文件,它调用测试(测试文件)并区分我们是否在 cran 上:

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

使用环境变量,like testthat does:

skip_on_cran <- function() {
  if (identical(Sys.getenv("NOT_CRAN"), "true")) {
    return(invisible(TRUE))
  }

  skip("On CRAN")
}

是的!您可以以编程方式自动处理此问题。让我详细说明我设置的两种方式:

隐含地通过版本号:这是 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")
}

本质上,我们测试版本是否为 a.b.c.d 形式——如果是,则断定它是开发版本。这意味着“运行 所有测试”。而 a.b.c 形式的发布版本将转到 CRAN,而不是 运行 这些测试,因为它们会超过他们的时间限制。

在每个 actual unit test files 中,我们然后可以决定是否要遵守变量并跳过测试是否已设置,或者仍然执行:

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

if (.runThisTest) {

   ## code here that contains the tests

}

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

我还是很喜欢这种方式。

明确地通过资源文件我们中的一些人正在处理的另一个包(最近很多)需要一个特定的后端可用。因此,在 Rblpapi 包中,我们测试是否存在一个文件,我和我的合著者都在我们的 $HOME 目录下,以便设置凭据和连接详细信息。如果文件丢失---例如在 Travis CI、CRAN 或其他用户上,测试被跳过。

我们选择使用资源文件作为R文件;它 sources it if found and thereby sets values for 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

}

与第一个用例类似,我们现在可以设置更多变量,稍后进行测试。

通过 Travis 显式 CI 我们在 rfoaas 中使用的另一个选项是在 Travis CI file:

中设置管理它的环境变量
env:
  global:
    - RunFOAASTests=yes

其中 tests script then picks up:

## 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

在这种情况下,我还根据我的用户 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

通过另一个变量显式 你当然也可以依赖你在所有包中使用的另一个变量。我觉得那是个坏主意。如果您在 shell 中设置此设置,则在包 A 上工作并将其设置为抑制测试,然后切换到包 B --- 您可能会忘记取消设置变量,然后无法测试。我最不喜欢这种方法,不使用它。