Testthat package testing Phuse Non-clinical Scripts
Prerequisites install.packages("roxygen2") install.packages("devtools") install.packages("testthat") Download and install Rtools 3.4 from http://cran.r-project.org/bin/windows/Rtools/
Devtools workflow for package: library(devtools) build() install() document() test() Will run the “testthat” test case files, from tests subdirect. To get the pdf manual: check(cleanup = FALSE,manual = TRUE,path = getwd()) http://kbroman.org/pkg_primer/pages/docs.html
Rstudio New project – From Build pulldown Create project from existing directory From Build pulldown Build and reload -> does compile,loading Test -> runs the testthat scripts Check -> does build, testthat and checks documentation
Rstudio
Rstudio – check of parsedate Results: Undocumented arguments in documentation object 'dur_to_seconds' 'input' Documented arguments not in \usage in documentation object 'dur_to_seconds': 'date' checking package dependencies ... NOTE Package suggested but not available for checking: 'covr'
Rstudio – check of parsedate Remedied: Changed from: #' @param date The date(s) to format. To #' @param input The date(s) to format. install.packages("covr")
Parsedate – tests directory “testthat” test case files http://kbroman.org/pkg_primer/pages/docs.html
Testthat test cases “Test” from Build pulldown: ==> devtools::test() Loading parsedate Loading required package: testthat Testing parsedate Dates and empty strings: ....... Git dates: ........... ISO 8601 moment.js: ................................. ISO 8601 Pelago: ...................................... ISO 8601 Pelago non-matching: ....................... ISO week dates: ... DONE =========================================================================== Warning message: package 'testthat' was built under R version 3.2.5 http://kbroman.org/pkg_primer/pages/docs.html
Testthat test cases in parsedate Testthat.r: library(testthat) library(parsedate) test_check("parsedate") http://kbroman.org/pkg_primer/pages/docs.html
Testthat test cases in parsedate http://kbroman.org/pkg_primer/pages/docs.html
Testthat documentation Context – name under which to group tests test_that(name,code) – create a test Expect_equal – checks if 2 values are the same See documentation for other types of equality-expectations Also see document for “mock” usage for complicated out-of-package function substitution http://kbroman.org/pkg_primer/pages/docs.html
Testthat parsedate example context("ISO week dates") test_that("Exotic ISO week dates are OK", { tests <- read.table(stringsAsFactors = FALSE, header = FALSE, strip.white = TRUE, row.names = NULL, sep = "|", textConnection(" 2009-W01-1 | 2008-12-29T00:00:00+00:00 2009-W53-7 | 2010-01-03T00:00:00+00:00 2013-W06-5 | 2013-02-08T00:00:00+00:00" )) apply(tests, 1, function(x) { d <- format_iso_8601(parse_iso_8601(x[1])) expect_equal(d, unname(x[2])) }) Expected output http://kbroman.org/pkg_primer/pages/docs.html Input to parse function
Testthat parsedate - boundary checks (corner cases) also done test_that("Non-sensical input is removed", { date <- "?=)(!$§#$%" expected <- as.POSIXct(NA) actual <- parse_date(date) expect_equal(expected, actual) }) http://kbroman.org/pkg_primer/pages/docs.html
Testthat added as a test context("BJF examples") library(stringr) test_that("Example 1 BJF", { tests <- read.table(stringsAsFactors = FALSE, header = FALSE, strip.white = TRUE, row.names = NULL, sep = "|", textConnection(" 2013-02-08T09:30:26.123 | 2013-02-08T10:30:26+00:00 2013-02-08T09:30:26.123 | 2013-02-08T09:30:26+00:00 " )) apply(tests, 1, function(x) { d <- format_iso_8601(parse_iso_8601(x[1])) expect_equal(d, unname(x[2]), info = x[1]) }) http://kbroman.org/pkg_primer/pages/docs.html
Testthat added as a test test_that("Example for BJF", { tests <- read.table(stringsAsFactors = FALSE, header = FALSE, strip.white = TRUE, row.names = NULL, sep = "|", textConnection(" The sky is blue | red The sky is blue | blue " )) apply(tests, 1, function(x) { d <- (word(x[1],-1)) expect_equal(d, unname(x[2]), info = x[1]) }) http://kbroman.org/pkg_primer/pages/docs.html
Testthat added as a test - output ==> devtools::test() Loading parsedate Loading required package: testthat Testing parsedate BJF examples.js: 1.2. Dates and empty strings: ....... Git dates: ........... ISO 8601 moment.js: ................................. ISO 8601 Pelago: ...................................... ISO 8601 Pelago non-matching: ....................... ISO week dates: ... Failed ------------------------------------------------------------------------- 1. Failure: Examples for BJF.js (@test-bjfexample.R#16) ------------------------ `d` not equal to unname(x[2]). 1/1 mismatches x[1]: "2013-02-08T09:30:26+00:00" y[1]: "2013-02-08T10:30:26+00:00" 2013-02-08T09:30:26.123 2. Failure: Examples for BJF.js (@test-bjfexample.R#32) ------------------------ x[1]: "blue" y[1]: "red" The sky is blue http://kbroman.org/pkg_primer/pages/docs.html
Testthat - describe describe(function,code) – description of function and its test (alternative function in test_that package) http://kbroman.org/pkg_primer/pages/docs.html
Testthat – describe added as test describe("parse_iso_8601()", { it ("can convert fractional hours and time zone offset",{ tests <- read.table(stringsAsFactors = FALSE, header = FALSE, strip.white = TRUE, row.names = NULL, sep = "|", textConnection(" 2010-02-18T16:23.33+0600 | 2010-02-18T10:23:19+00:00 " )) apply(tests, 1, function(x) { d <- format_iso_8601(parse_iso_8601(x[1])) expect_equal(d, unname(x[2]), info = x[1]) }) http://kbroman.org/pkg_primer/pages/docs.html
Q&A Alphabetically within that “testthat” directory How does Testthat determine which files to run? In Rstudio, under project -> build -> test package runs: devtools::test() This command, by defaults, looks for a subdirectory “tests” And within that a subdirectory “testthat” Files found that are within the “testthat” directory and start with “test” are run. What are the order of the files run? Alphabetically within that “testthat” directory What naming conventions must be followed? Test files must start with “test” Convention (not mandatory) is that they start with “test-” They must end with a “r” or “R” http://kbroman.org/pkg_primer/pages/docs.html