Presentation is loading. Please wait.

Presentation is loading. Please wait.

Testthat package testing Phuse Non-clinical Scripts

Similar presentations


Presentation on theme: "Testthat package testing Phuse Non-clinical Scripts"— Presentation transcript:

1 Testthat package testing Phuse Non-clinical Scripts

2 Prerequisites install.packages("roxygen2")
install.packages("devtools") install.packages("testthat") Download and install Rtools 3.4 from

3 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())

4 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

5 Rstudio

6 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'

7 Rstudio – check of parsedate
Remedied: Changed from: date The date(s) to format. To input The date(s) to format. install.packages("covr")

8 Parsedate – tests directory
“testthat” test case files

9 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

10 Testthat test cases in parsedate
Testthat.r: library(testthat) library(parsedate) test_check("parsedate")

11 Testthat test cases in parsedate

12 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

13 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 | T00:00:00+00: W53-7 | T00:00:00+00: W06-5 | T00: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 Input to parse function

14 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) })

15 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(" T09:30: | T10:30:26+00: T09:30: | T09: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]) })

16 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]) })

17 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 Failure: Examples for BJF.js `d` not equal to unname(x[2]). 1/1 mismatches x[1]: " T09:30:26+00:00" y[1]: " T10:30:26+00:00" T09:30: Failure: Examples for BJF.js x[1]: "blue" y[1]: "red" The sky is blue

18 Testthat - describe describe(function,code) – description of function and its test (alternative function in test_that package)

19 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(" T16: | T10: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]) })

20 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”


Download ppt "Testthat package testing Phuse Non-clinical Scripts"

Similar presentations


Ads by Google