An R-Ladies Philly workshop on unit testing on Nov 11, 2021. An accompanying blog post and workshop recording are now available.
In this workshop, Shannon Pileggi and Gordon Shotwell discuss how to get started with unit testing in R, which is formal automated testing of functions within packages. We demonstrate handy functions in usethis and devtools, strategies for writing tests, debugging techniques, and broad concepts in function writing that facilitate a smoother testing process.
This workshop picks up exactly where we left our little ralph (aka R-Ladies Philly) package one year ago with “Your first R package in 1 hour: Tools that make R package development easy”. Participants will get the most out of this workshop if they review those materials in advance, or if they are already familiar with building R packages with devtools and usethis.
Please install, or update, the following packages in advance of the workshop:
-
usethis,devtools,testthat,covr, andDT -
broom,glue -
dplyr,purrr,magrittr, andrlang(ortidyverse)
You can work either (1) locally or (2) with GitHub.
-
To work locally, click the green
Codebutton and download the zippedralphGetsTestedrepository. -
To work through GitHub, fork and clone this repository with
usethis::create_from_github("shannonpileggi/ralphGetsTested").
-
Ctrl + Sfor save file -
Ctrl + Shift + Lfordevtools::load_all() -
Ctrl + Shift + F10to restart R
-
R package developement cheat sheet
-
Ch. 12 Testing in R packages by Hadley Wickham and Jenny Bryan
-
Ch. 8.2 Signalling conditions in Advanced R by Hadley Wickham
-
{usethis} user interface functions
-
Debugging
-
RStudio blog post by Jonathan McPherson Debugging with the RStudio IDE
-
Ch 22 Debugging in Advanced R by Hadley Wickham
-
-
Indrajeet Patil's curated list of awesome tools to assist R package development.
Review the compute_corr() function and the available expectations in testthat.
-
What can we test in
compute_corr()? -
What should we test in
compute_corr()? -
How much of the function code is covered by tests?
Add the awesome_rladies function to the package.
awesome_rladies <- function(v) {
sapply(v, function(x) {
if (x == 1) {
verb <- "is"
noun <- "RLady"
}
if (x > 1) {
verb <- "are"
noun <- "RLadies"
}
as.character(glue::glue("There {verb} {x} awesome {noun}!"))
})
}
and execute the function with:
awesome_rladies(1)
awesome_rladies(2)
awesome_rladies(1:2)
-
Can we break this up to make it easier to test?
-
What type of object should the function output?
-
What type of object does this function expect? Can we put up guardrails so the user doesn’t send the wrong thing? How do we test those guardrails?