使用 check() 测试失败

testthat failure using check()

我正在尝试向我的程序包添加一些测试,以确保在我进行更改时一切正常。我在执行此操作时遇到了一些困难。

我想测试一下我的包的主要功能,大致可以说是一种插补法。因此,如果我给它一个 n x 2 矩阵 Y,其中第二列有一些 NA,它应该 return Z 其中第一列 [=13] =] 和 Z 相同(因为它被完全观察到)并且应该估算第二列,以便 Z 的第二列中没有 NA

很明显,函数还有其他几个输入,但我测试的主要结构是

context("Test output")
test_that("First column equal", {
  set.seed(100)
  Y <- matrix(rnorm(200), 100, 2)
  Y[seq(1, 100, by = 3), 2] <- NA
  out <- my_fun(Y)
  expect_equal(Y[, 1], out[, 1])
})

我的问题是这不起作用。它在我 运行 devtools::test() 时有效,但在 运行 宁 devtools::check() 时无效。我尝试使用 expect_equal_to_reference()(因为我真正想要测试的比这个例子更大更复杂),但它也会抛出一个错误,尽管 运行 在控制台中输入代码并与保存的代码进行比较.rds 文件显示它们是相同的。

我找到了 Hadley (under tests) 的这句话:

Occasionally you may have a problem where the tests pass when run interactively with devtools::test(), but fail when in R CMD check. This usually indicates that you’ve made a faulty assumption about the testing environment, and it’s often hard to figure it out.

这不是什么好兆头,但我能做什么呢?有任何想法吗?

这是我得到的错误(test_file 是包含上述代码的文件的名称):

checking tests ...
** running tests for arch 'i386' ... ERROR
Running the tests in 'tests/testthat.R' failed.
Last 13 lines of output:
  3: asNamespace(ns)
  4: getNamespace(ns)
  5: tryCatch(loadNamespace(name), error = function(e) stop(e))
  6: tryCatchList(expr, classes, parentenv, handlers)
  7: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  8: value[[3L]](cond)
... 13 lines ...
  5: tryCatch(loadNamespace(name), error = function(e) stop(e))
  6: tryCatchList(expr, classes, parentenv, handlers)
  7: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  8: value[[3L]](cond)

  testthat results ================================================================
  OK: 0 SKIPPED: 0 FAILED: 1
  1. Error: First column equal (@test_file.R#12) 

答案既简单又令人尴尬。该包需要加载到测试文件中,因此它应该以:

开头
library(mypackage)
context("Test output")

虽然我以前认为它有效,但实际上没有。现在包已正确加载,我可以看到例如my_fun 打印的进度条出现在构建窗格中。