Nov 18, 2026
|
59 min
|
1.2k views
How do you get started with unit testing in R? | Jacci Ziebert | Data Science Lab
Start small: put your code in small functions inside a package, run use_testthat() to set up testing, and write one test_that() test that compares a function’s output to what you expect. Then try edge cases like zero or negative input, and use assertthat or cli for clear error messages. You’re probably testing informally already, so this just writes it down, and you can add snapshots, mocking, or the ensure add-in later.
In this Data Science Lab session, Jacci Ziebert, facilitate a community discussion on the fundamentals and importance of unit testing in R. We begin by addressing the common practice of writing monolithic functions and why modularity is essential for easier debugging and more reliable testing. Jacci does this by showcasing the use of the testthat package to create and run unit tests.
The Data Science Lab is a live weekly call. Register at pos.it/dslab! Discord invites go out each week on lives calls. We’d love to have you! It’s is an open, messy space for learning and asking questions. Think of it like pair coding with a friend or two. Learn something new, and share what you know to help others grow.
Originally recorded on October 6, 2026.
Resources mentioned:
Jacci’s unit testing blog post: https://jacciz.github.io/portfolio/projects/unit_testing.html Jacci’s blog post on clean code: https://jacciz.github.io/portfolio/projects/clean_code.html usethis: https://usethis.r-lib.org/ testthat: https://testthat.r-lib.org/ assertthat: https://github.com/hadley/assertthat waldo: https://waldo.r-lib.org/ glue: https://glue.tidyverse.org/ rlang: https://rlang.r-lib.org/ withr: https://withr.r-lib.org/ ensure: https://simonpcouch.github.io/ensure/ cli: https://cli.r-lib.org/reference/index.html cli progress bar! https://cli.r-lib.org/reference/cli_progress_step.html echarts4r: https://echarts4r.john-coene.com/ Positron keybindings: https://positron.posit.co/keyboard-shortcuts.html Plotly for R: https://plotly.com/r/ Snapshot testing in R: https://testthat.r-lib.org/articles/snapshotting.html autogradeR: https://math-mcshane.github.io/autogradeR/ Ryan McShane’s unit testing for student code: https://ryanmcshane.com/talk/ecots_2026/ Riffomonas/ Pat Schloss’ videowhere you write the test failing tests first: https://www.youtube.com/watch?v=TaNvqwMmHus Custom expectations vignette in testthat: https://testthat.r-lib.org/articles/custom-expectation.html Skill for testing R packages: https://github.com/posit-dev/skills/tree/main/r-lib/testing-r-packages 0:00 Welcome to the Data Science Lab 1:09 Poll: Do you do testing in R? 3:21 Testober: what’s coming (Python and Shiny testing) 4:35 Why unit test? The “monolith function” problem 9:18 Why modularity and testability help (Jacci’s story) 10:56 Q&A: Does input validation count as testing? 12:54 Jacci shares her screen: building a test-friendly package 13:47 Tip: add a Source column in RStudio 17:06 Helpful packages: usethis, testthat, assertthat, cli 18:32 Q&A: Package development shortcuts in Positron 20:23 What a unit test is and setting up with use_testthat() 23:52 Writing your first tests and testing edge cases 31:44 Clearer error messages with assertthat 35:35 Testing that errors and messages show up (expect_error) 38:24 Snapshot testing with expect_snapshot() 43:11 Q&A: Embedding values in error messages, and cli 46:41 Q&A: Reusing tests and mocking functions 50:22 Generating unit tests with the ensure add-in 55:23 Agent skills for testing R packages 58:22 Wrap-up and thank you to Jacci
cli
Positron
rlang
rstudio
Shiny
testthat
tidyverse
tidyverse.org
usethis
waldo
withr