1

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.

Community
  • 1
  • 1
Victor K.
  • 4,054
  • 3
  • 25
  • 38
  • 3
    See [Using data.table package inside my own package](http://stackoverflow.com/questions/10527072/using-data-table-package-inside-my-own-package) - You aren't declaring dependencies properly – mnel Mar 03 '14 at 23:59
  • 2
    @mnel is correct. You don't have data.table listed as a dependency anywhere https://github.com/victorkryukov/ttdt/blob/master/DESCRIPTION – Dason Mar 04 '14 at 00:03
  • @mnel - I doubt the dependency is the problem. I've set dependencies correctly in https://github.com/victorkryukov/ttdt/commit/755bf8a803abc57fe1480b78eaf1eb72da7633e3 and I still observe this bug. – Victor K. Mar 04 '14 at 00:21
  • @Dason - see my comment above. – Victor K. Mar 04 '14 at 00:24
  • 1
    @VictorK. - use `fresh=TRUE` as well.... see my answer – mnel Mar 04 '14 at 00:28

1 Answers1

3

If you

  1. Set the dependences properly in the DESCRIPTION file
  2. Run test(fresh = TRUE)

Everything should work.

Using fresh = TRUE will ensure the tests are run in a fresh R session.

Victor K.
  • 4,054
  • 3
  • 25
  • 38
mnel
  • 113,303
  • 27
  • 265
  • 254