R - data.table and testthat package

I am creating a package that works with data.table and which should be tested with a test package. Although the code works fine when invoked from the command line, I run into problems when invoked from a test case. It seems that the function [] from the base package, i.e. The function for data.frames is used when running tests.

I created a minimal example that can be found here: https://github.com/utalo/test_datatable_testthat

The package contains one function:

test <- function() { dt <- data.table(MESSAGE="Test 1234567890",TYPE="ERROR") dt[,.(MESSAGE=strwrap(MESSAGE,width = 10)),by=.(TYPE)] } 

When calling test.datatable.testthat:::test() from the command line, I get the expected result:

  TYPE MESSAGE 1: ERROR Test 2: ERROR 1234567890 

However, when performing the following unit test:

 test_that("Test package",{ dt <- test() expected_res <- structure(list(TYPE = c("ERROR", "ERROR"), MESSAGE = c("Test","1234567890")), row.names = c(NA, -2L), class = c("data.table","data.frame"), .Names = c("TYPE", "MESSAGE")) expect_equal(dt,expected_res) }) 

I get an error message:

 1 1. Error: Test package ------------------------------------------------------------------------------------------------------- could not find function "." 1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage")) 2: eval(code, new_test_environment) 3: eval(expr, envir, enclos) 4: test() at test.R:4 5: dt[, .(MESSAGE = strwrap(MESSAGE, width = 10)), by = .(TYPE)] at test.datatable.testthat/R/hello.R:5 6: `[.data.table`(dt, , .(MESSAGE = strwrap(MESSAGE, width = 10)), by = .(TYPE)) at C:\Users\D057806\Documents\R\test.datatable.testthat/R/hello.R:5 7: `[.data.frame`(x, i, j) 

As you can see, inside the test, [] data.frame is called. My first assumption was that the dependency on the data.table package was not declared correctly. This is my DESCRIPTION file:

 Package: test.datatable.testthat Type: Package Title: What the Package Does (Title Case) Version: 0.1 Date: 2016-04-07 Authors@R : person("First", "Last", email = " first.last@example.com ", role = c("aut", "cre")) Description: More about what it does (maybe more than one line) License: What license is it under? LazyData: TRUE Depends: data.table Suggests: testthat RoxygenNote: 5.0.1 

According to Using the data.table package inside my own package , this should be enough to declare data.table as a dependent package. However, this does not seem to be the case.

Any hints as to why my function works when called directly, but not in the context of testthat?

+6
source share
1 answer

To comment on this post, send a reply.

  • Do not use the underscore in the package name; it violates the standard. The underscores will be rotated to dots.

  • I can not say why testthat could not process your test. You can try to export the test function. It is not exported, therefore it should only be used with ::: explicitly. Maybe testthat depends on this in some way, I don't know.

  • The test passes when I transfer the tests from the test. If you can’t solve it, I will look for support in the tests.

You can see my plugin jangorecki / test_datatable_testthat of your pkg (the URL will not work in a few days, so bring changes if you want to access them later).
Your test passed from the test, which is in tests/test.R , below.

 dt <- test.datatable.testthat:::test() expected_res <- structure(list(TYPE = c("ERROR", "ERROR"), MESSAGE = c("Test","1234567890")), row.names = c(NA, -2L), class = c("data.table","data.frame"), .Names = c("TYPE", "MESSAGE")) stopifnot(all.equal(dt,expected_res)) 

Testthat is suppressed by changing it to a dummy, type TRUE==TRUE . Now your test, defined outside the test, has passed OK.
Related part from 00check.log :

 * checking tests ... Running 'test.R' Running 'testthat.R' OK * DONE 
0
source

All Articles