Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testthat failure using check()

I'm trying to add some testing to my package to make sure that things remain as they should when I make changes. I'm having some difficulties in doing this.

I want to test the main function of my package, which can be roughly described as an imputation method. So if I give an n x 2 matrix Y to it where the second column has some NA, it should return Z where the first columns in Y and Z are the same (because it's completely observed) and the second column should be imputed so that there are no NA in the second column of Z.

Obviously, there are several other inputs to the function, but the main structure of my test is

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])
})

My problem is that this doesn't work. It works when I run devtools::test(), but not when running devtools::check(). I tried using expect_equal_to_reference() (because what I really want to test is bigger and more compelx than this example), but it also throws an error, despite running the code in the console and comparing to the saved .rds file shows that they are identical.

I found this quote by 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.

It doesn't bode well, but what can I do? Any ideas?

Here is the error I get (test_file is the name of the file containing the above code):

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) 
like image 682
hejseb Avatar asked Oct 18 '22 15:10

hejseb


1 Answers

The answer is as embarrassing as it is simple. The package needs to be loaded in the test file, so that it should start with:

library(mypackage)
context("Test output")

While I thought that it worked before, it actually did not. Now that the package is loaded properly, I can see that e.g. the progress bar printed by my_fun appears in the Build pane.

like image 116
hejseb Avatar answered Oct 21 '22 15:10

hejseb