Sto costruendo un pacchetto che funziona con data.table e che deve essere testato utilizzando il pacchetto testthat. Mentre il codice funziona correttamente quando si chiama dalla riga di comando, mi imbatto in problemi quando si chiama da un caso di test. Sembra che la funzione [] dal pacchetto base, ovvero la funzione per data.frames, venga utilizzata durante l'esecuzione dei test.r - data.table e testthat pacchetto
ho creato un esempio minimo che può essere trovato qui: https://github.com/utalo/test_datatable_testthat
Il pacchetto contiene una sola funzione:
test <- function() {
dt <- data.table(MESSAGE="Test 1234567890",TYPE="ERROR")
dt[,.(MESSAGE=strwrap(MESSAGE,width = 10)),by=.(TYPE)]
}
Quando si chiama test.datatable.testthat:::test()
dalla riga di comando ottengo il risultato atteso:
Tuttavia, quando si esegue il seguente test dell'unità:
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)
})
ottengo un errore:
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)
Come si può vedere, all'interno del test del [] di data.frame si chiama. La mia prima ipotesi è che la dipendenza dal pacchetto data.table non sia dichiarata correttamente. Questo è il mio file di descrizione:
Package: test.datatable.testthat
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1
Date: 2016-04-07
[email protected]: person("First", "Last", email = "[email protected]", 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
Secondo Using data.table package inside my own package dovrebbe essere sufficiente per dichiarare data.table come un pacchetto dipendente. Tuttavia, questo non sembra essere il caso qui.
Eventuali indizi sul motivo per cui la mia funzione funziona quando viene chiamato direttamente ma non nel contesto di testthat?
Il problema persiste se si * * import data.table, e aggiungere 'import (data.table)' per lo spazio dei nomi? Chiedo perché [questo post] (http://stackoverflow.com/a/23279604/559784) viene in mente .. e sto indovinando che è lo stesso problema con 'testthat'. – Arun
@Arun Grazie per il puntatore! Questo sembra essere parte del problema. Se cambio l'istruzione dipende per importarlo, funziona nell'esempio di test, ma non nel mio codice originale. Proverò a riprodurre questo comportamento anche nell'esempio. – utal
Questo è nuovo .. Hai anche aggiunto import (data.table) al tuo NAMESPACE? – Arun