My question is similar to r devtools test() errors but testthat test_file() works, however I'm not sure whether @hadley's comment applies here as well.
I've created a minimal working example of an R package where testing individual file with testthat::test_file
works, but testing the package with devtools::test
does not.
Here is my package's only R file R/a.R
:
#' @export
generate_data_table <- function() {
data.table(a = 1:10, b = 11:20)
}
Here is my test file inst/tests/test-a.R
:
test_that("everything is OK", {
x <- generate_data_table()
expect_equal(x[b == 11]$a, 1)
})
That test passes when I run test_file
, but when I run devtools::test
I receive the following error:
> test()
Testing ttdt
Loading ttdt
1
1. Error: everything is OK -----------------------------------------------------
object 'b' not found
1: expect_equal(x[b == 11]$a, 1) at test-a.R:3
2: expect_that(object, equals(expected, label = expected.label, ...), info = info, label = label)
3: condition(object)
4: compare(expected, actual, ...)
5: compare.default(expected, actual, ...)
6: all.equal(x, y, ...)
7: all.equal.numeric(x, y, ...)
8: attr.all.equal(target, current, tolerance = tolerance, scale = scale, ...)
9: mode(current)
10: x[b == 11]
11: `[.data.table`(x, b == 11)
12: `[.data.frame`(x, i)
Is it a proper behavior or should it be considered a bug in data.table
or devtools
?
Here is my environment:
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.9.2 testthat_0.8.1 devtools_1.4.1
loaded via a namespace (and not attached):
[1] digest_0.6.4 evaluate_0.5.1 httr_0.2 memoise_0.1 parallel_3.0.2
[6] plyr_1.8.1 Rcpp_0.11.0 RCurl_1.95-4.1 reshape2_1.2.2 stringr_0.6.2
[11] tools_3.0.2 whisker_0.3-2
Update. I've updated dependencies of the package to correctly depend on data.table
here, but the problem still exists.
If you
DESCRIPTION
filetest(fresh = TRUE)
Everything should work.
Using fresh = TRUE
will ensure the tests are run in a fresh R session.