RUnit:找不到函数 "checkEquals"
RUnit: could not find function "checkEquals"
我正在创建一个具有标准目录层次结构的 R 包。在 R
目录中,我创建了一个 test
子目录。
在 R
目录中,我创建了一个 uTest.R
文件,其中包含:
uTest <- function() {
test.suite <- defineTestSuite('test',
dirs = file.path('R/test'))
test.result <- runTestSuite(test.suite)
printTextProtocol(test.result)
}
在 R/test
目录中,我创建了一个 runit.test.R
文件,其中包含:
test.validDim <- function() {
testFile <- "test/mat.csv"
generateDummyData(testFile,
10,
10)
checkEquals(validDim(testFile), TRUE)
}
我在 Rstudio 中使用 R CMD INSTALL --no-multiarch --with-keep.source RMixtComp
构建和安装我的包。当我尝试启动函数 uTest()
时,我收到此错误消息:
1 Test Suite :
test - 1 test function, 1 error, 0 failures
ERROR in test.validDim: Error in func() : could not find function "checkEquals"
但是,如果我在调用 uTest()
之前调用 library(RUnit)
,一切正常。在 DESCRIPTION
文件的 import
字段中,我添加了 RUnit
,在 NAMESPACE
文件中,我添加了 import(RUnit)
.
如何在加载包后直接调用 uTest()
,而无需手动加载 RUnit?
您不应将 RUnit 添加到 DESCRIPTION 文件中的 Depends(或 Imports)字段(尽管有相反的注释)。这样做意味着 RUnit 包是使用您的包所必需的,但事实可能并非如此。换句话说,将 RUnit 放在 Depends 或 Imports 中意味着需要安装 RUnit (Imports) 并在用户的搜索路径 (Depends) 上,以便他们使用您的包。
您应该将 RUnit 添加到 DESCRIPTION 文件中的 Suggests 字段,然后修改您的 uTest
函数,如下所示:
uTest <- function() {
stopifnot(requireNamespace("RUnit"))
test.suite <- RUnit::defineTestSuite('test', dirs = file.path('R/test'))
test.result <- RUnit::runTestSuite(test.suite)
RUnit::printTextProtocol(test.result)
}
这样做允许您使用 RUnit 进行测试,但不需要用户安装 RUnit(并且可能在他们的搜索路径上)才能使用您的包。显然,如果他们希望 运行 您的测试,他们将需要 RUnit。
我正在创建一个具有标准目录层次结构的 R 包。在 R
目录中,我创建了一个 test
子目录。
在 R
目录中,我创建了一个 uTest.R
文件,其中包含:
uTest <- function() {
test.suite <- defineTestSuite('test',
dirs = file.path('R/test'))
test.result <- runTestSuite(test.suite)
printTextProtocol(test.result)
}
在 R/test
目录中,我创建了一个 runit.test.R
文件,其中包含:
test.validDim <- function() {
testFile <- "test/mat.csv"
generateDummyData(testFile,
10,
10)
checkEquals(validDim(testFile), TRUE)
}
我在 Rstudio 中使用 R CMD INSTALL --no-multiarch --with-keep.source RMixtComp
构建和安装我的包。当我尝试启动函数 uTest()
时,我收到此错误消息:
1 Test Suite :
test - 1 test function, 1 error, 0 failures
ERROR in test.validDim: Error in func() : could not find function "checkEquals"
但是,如果我在调用 uTest()
之前调用 library(RUnit)
,一切正常。在 DESCRIPTION
文件的 import
字段中,我添加了 RUnit
,在 NAMESPACE
文件中,我添加了 import(RUnit)
.
如何在加载包后直接调用 uTest()
,而无需手动加载 RUnit?
您不应将 RUnit 添加到 DESCRIPTION 文件中的 Depends(或 Imports)字段(尽管有相反的注释)。这样做意味着 RUnit 包是使用您的包所必需的,但事实可能并非如此。换句话说,将 RUnit 放在 Depends 或 Imports 中意味着需要安装 RUnit (Imports) 并在用户的搜索路径 (Depends) 上,以便他们使用您的包。
您应该将 RUnit 添加到 DESCRIPTION 文件中的 Suggests 字段,然后修改您的 uTest
函数,如下所示:
uTest <- function() {
stopifnot(requireNamespace("RUnit"))
test.suite <- RUnit::defineTestSuite('test', dirs = file.path('R/test'))
test.result <- RUnit::runTestSuite(test.suite)
RUnit::printTextProtocol(test.result)
}
这样做允许您使用 RUnit 进行测试,但不需要用户安装 RUnit(并且可能在他们的搜索路径上)才能使用您的包。显然,如果他们希望 运行 您的测试,他们将需要 RUnit。