I am trying to add some testing to my package to make sure everything remains as it should when I make changes. I have some difficulties with this.
I want to check the main function of my package, which can be roughly described as an imputation method. Therefore, if I give the matrix nx 2 Y , where the second column has some NA , it should return Z , where the first columns in Y and Z coincide (since it is fully respected), and the second column should be imputed so that in the second column Z does not was NA .
Obviously, there are several other inputs to the function, but the basic structure of my test
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 does not work. It works when I run devtools::test() , but not when I run devtools::check() . I tried using expect_equal_to_reference() (because what I really want to check is bigger and more complex than this example), but it also throws an error despite running the code in the console and comparing with the saved .rds file shows that they are identical .
I found this Hadley quote ( in tests ):
Sometimes you may have a problem when tests pass when starting interactively with devtools :: test (), but the R CMD fails to validate. This usually indicates that you made the erroneous assumption about and it is often difficult to understand.
It does not 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)