检索期望的data.frame以测试期望值

时间:2015-04-08 04:25:23

标签: r unit-testing dataframe testthat

我想测试一个函数返回预期的data.frame。 data.frame太大而无法在R文件中定义(例如,使用structure()之类的东西)。当我尝试从磁盘进行简单检索时,我对环境做错了,例如:

test_that("SO example for data.frame retreival", {   
  path_expected <- "./inst/test_data/project_longitudinal/expected/default.rds"
  actual <- data.frame(a=1:5, b=6:10) #saveRDS(actual, file=path_expected)
  expected <- readRDS(path_expected)
  expect_equal(actual, expected, label="The returned data.frame should be correct")
})

在控制台中运行时,行正确执行。但是当我运行devtools::test()时,从文件中读取rds / data.frame时会发生以下错误。

1. Error: All Records -Default ----------------------------------------------------------------
cannot open the connection
1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage"), 
       warning = function(c) invokeRestart("muffleWarning"))
2: eval(code, new_test_environment)
3: eval(expr, envir, enclos)
4: readRDS(path_expected) at test-read_batch_longitudinal.R:59
5: gzfile(file, "rb")

为了完成这项工作,环境需要进行哪些调整?如果这不是一种简单的方法,那么测试大型数据框架的好方法是什么?

2 个答案:

答案 0 :(得分:2)

我建议您查看优秀的ensurer包。您可以在函数本身内包含这些函数(而不是testthat测试集的一部分)。 如果数据框(或您想要检查的任何对象)不能满足您的要求,它将抛出错误,并且只有在通过测试时才会返回该对象。 与testthat的区别在于ensurer是为了在运行时检查对象而构建的,这可能会绕过您面临的整个环境问题,因为对象是在运行时在函数内部进行测试的。 请参阅this vignette的末尾,了解如何根据您可以根据需要制作的模板测试数据框。您还可以在函数内部找到许多其他测试。在这种情况下,看起来这种方法可能优于testthat

答案 1 :(得分:0)

基于@Gavin Simpson的评论,问题不涉及环境,而是涉及文件路径。更改snippet's second line工作。

path_qualified <- base::file.path(
    devtools::inst(name="REDCapR"),
    test_data/project_longitudinal/expected/dummy.rds"
)  

找到文件的位置,无论我是以交互方式调试,还是测试正在运行(因此inst是否在路径中)。