2015-12-04 18 views
6

Sto lavorando al pacchetto PKNCA per R. Mentre sviluppavo il codice di test, alcuni dei test sarebbero anche dei buoni esempi. Voglio tenerli come entrambi (test ed esempio). C'è un modo in cui posso incorporare qualcosa nella documentazione di roxygen2 che verrà copiato anche nel test?Posso generare automaticamente test unitari per testthat dagli esempi di roxygen2?

Quello che sto pensando è la documentazione come:

#' @exampleTest 
#' set.seed(5) 
#' rnorm(1) ## -0.8409 

E che avrebbe generato un test come:

expect_equal({set.seed(5) 
       rnorm(1)}, -0.8409, tol=1e-4) 

(Il tol deriva dal fatto che si tratta di un numero e la numero di cifre mostrato nell'esempio.)

risposta

2

Utilizzare devtools::run_examples() come spiegato in the check chapter of Hadley Wickham's book on packages. Gli esempi di funzioni vengono testati quando si esegue R CMD CHECK. Questo non fa parte del test ma piuttosto del sistema di controllo del pacchetto R standard.

+0

Leggendo la documentazione, questo sembra che confermerà che gli esempi correvano senza errori, ma non apparire come esso deve svolgere test di precisione. Immagino di poter eseguire i miei esempi dove richiedono precisione, ma questo non lo inserisce direttamente nel framework di test. Se non ci sono altre risposte imminenti, imposterò questa come risposta. Ma, spero anche in un modo per confermare l'accuratezza degli esempi. –

+0

Più direttamente, 'testthat :: test_examples' eseguirà gli esempi nel pacchetto durante il test. I test passano quando non ci sono errori durante l'esecuzione degli esempi. –

1

C'è un modo, ma non è così liscio come vorresti. Dovrai chiamare le funzioni testthat all'interno del tuo blocco @examples. Ecco un esempio di funzione:

#' @examples 
#' testStrings <- c("1234567890", 
#'     "123 456 7890") 
#' 
#' testthat::expect_equal(extractPhoneNumbers(testStrings), "0123") 
extractPhoneNumbers <- function(inputStr) { 
    # check input: 
    if (!is.character(inputStr)) { 
     stop("'inputStr' must be a (vector of) string(s)!") 
    } 

    # imports 
    `%>%` <- stringr::`%>%` 
    replace_all <- stringr::str_replace_all 
    extract_all <- stringr::str_extract_all 

    # intermediary regex's 
    visualDelimitersRegex <- "[()+\\-_. ]" 
    phoneNumberRegex <- "[:digit:]{10}" 

    inputStr %>% 
    replace_all(pattern = visualDelimitersRegex, replacement = "") %>% 
    extract_all(pattern = phoneNumberRegex) 
} 

Quando si esegue devtools::run_examples() o devtools::check, entrambi saranno gettare errori in quanto la chiamata a testthat::expect_equal() genera un errore.

Esempio di output da devtools::check sembra

*** SNIP *** 
* checking for unstated dependencies in examples ... OK 
* checking examples ... ERROR 
Running examples in ‘demoPkg-Ex.R’ failed 
The error most likely occurred in: 

> base::assign(".ptime", proc.time(), pos = "CheckExEnv") 
> ### Name: extractPhoneNumbers 
> ### Title: Extract Phone Numbers 
> ### Aliases: extractPhoneNumbers 
> 
> ### ** Examples 
> 
> testStrings <- c("1234567890", 
+     "123 456 7890") 
> 
> testthat::expect_equal(extractPhoneNumbers(testStrings), "0123") 
Error: extractPhoneNumbers(testStrings) not equal to "0123" 
Modes: list, character 
Length mismatch: comparison on first 1 components 
Component 1: 1 string mismatch 
Execution halted 
* checking for unstated dependencies in ‘tests’ ... OK 
* checking tests ... 
    Running ‘testthat.R’ 
OK 
* checking PDF version of manual ... OK 
* DONE 

Status: 1 ERROR 
+0

grazie per il suggerimento. Come hai suggerito, non è così liscio come vorrei. Dal momento che nasconderò l'esempio, non credo che lo userò, ma ho suggerito che l'uso di testthat con esempi potrebbe aiutare. –