是的!您可以以编程方式自动处理此问题。让我详细说明我设置的两种方式:
隐含地通过版本号:这是 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——你可能会忘记取消设置变量,然后测试失败。我最不喜欢这种方法,不使用它。