I am building a package which works with data.table and which should be tested using package testthat. While the code works fine when calling from the command line, I run into issues when calling from a test case. It seems that the [] function from the base package, i.e. the function for data.frames is used when running the tests.
I have created a minimum example which can be found here: https://github.com/utalo/test_datatable_testthat
The package contains a single 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 executing 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:
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, within the test the [] of data.frame is called. My first guess was that the dependency to the data.table package is 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 data.table package inside my own package it should be sufficient to declare data.table as a dependent package. However, this does not seem to be the case here.
Any clues as to why my function is working when being called directly but not in the context of testthat?
To long for comment so posting as answer.
Don't use underscore in package name, it breaks the standard. Underscores will be turned to dots.
Can't really tell you why testthat failed to process your test. You can try to export
test
function. It is not exported so should be used only with:::
explicitly. Maybe testthat depends on that in some way, don't know.Test is passing when I move tests out of testthat. If you fail to resolve it I would look for support in testthat issues.
You can see my fork jangorecki/test_datatable_testthat of your pkg (url won't work in few days from now so fetch the changes if you want to access them later).
Your test moved out of testthat is in tests/test.R
, content 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 test is suppressed by changing it to dummy, kind of TRUE==TRUE
.
Now your test defined outside of testthat pass OK.
Related part from 00check.log
:
* checking tests ...
Running ‘test.R’
Running ‘testthat.R’
OK
* DONE
来源:https://stackoverflow.com/questions/36547235/r-data-table-and-testthat-package