2016-04-11 33 views
6

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?

+0

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

+0

@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

+0

Questo è nuovo .. Hai anche aggiunto import (data.table) al tuo NAMESPACE? – Arun

risposta

0

A lungo per il commento in modo da postare come risposta.

  1. Non utilizzare il carattere di sottolineatura nel nome del pacchetto, esso infrange lo standard. I caratteri di sottolineatura saranno trasformati in punti.

  2. Non posso davvero dirvi perché testthat non è riuscito a elaborare il test. Puoi provare ad esportare la funzione test. Non viene esportato, quindi deve essere utilizzato solo con ::: in modo esplicito. Forse il test dipende da questo in qualche modo, non lo so.

  3. Il test passa quando si spostano i test dal test. Se non riesci a risolverlo, cercherò il supporto per testare i problemi.

È possibile vedere la mia forchetta jangorecki/test_datatable_testthat del pkg (URL non funziona in pochi giorni da oggi in modo da recuperare i cambiamenti se si desidera accedere in un secondo momento).
Il test è stato rimosso dal test che si trova in tests/test.R, contenuto di seguito.

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 prova viene soppressa cambiandolo manichino, tipo di TRUE==TRUE. Ora il test è stato definito al di fuori del test che passa OK.
parte correlate da 00check.log:

* checking tests ... 
    Running ‘test.R’ 
    Running ‘testthat.R’ 
OK 
* DONE 
+0

ad 2. Ho testato con successo le funzioni non esportate prima, quindi non mi aspetto che il problema sia presente. annuncio 3. Ho dato un'occhiata a testthat problemi. Sembra esserci una discussione sul fatto che il problema correlato https://github.com/hadley/devtools/issues/192 sia stato risolto o meno, ad es. qui http://stackoverflow.com/questions/23252231/r-data-table-breaks-in-exported-functions – utal