【发布时间】:2015-10-23 15:06:56
【问题描述】:
问题:如何让testthat 在加载我的包而不是继承我的包的环境中运行?
背景:testthat 包在“从包的命名空间环境继承的环境中”运行测试[参见test_check 的文档]。这意味着它不能确保我正确地完成了我的导出,这让我很痛苦。
例如,我的包中有以下代码:
##' The foo() method
##' @param x object
##' @export
foo <- function(x)
UseMethod('foo')
##' @rdname foo
foo.data.frame <- function(x) {
message("foo data.frame")
}
##' @rdname foo
foo.default <- function(x) {
message("foo default")
}
我的测试中有以下内容:
x <- 5:13
foo(x)
测试很好。但是如果用户安装了这个包,他们会得到这个错误:
Error in UseMethod("foo") :
no applicable method for 'foo' applied to an object of class "c('integer', 'numeric')"
解决方案是为这两种方法添加@exports 声明,但测试没有捕捉到这一点真是太可惜了。
我更愿意从用户的角度运行我的所有测试,因为我有时会搞砸我的导出。或许可以向testthat:::run_tests 添加一个选项来选择所需的行为?
【问题讨论】:
标签: r namespaces testthat