Research Software Engineer, Biodiversity Futures Lab
2025-05-22
{medfiltr})Abbreviated from Wikipedia:
Unit testing is a form of software testing by which isolated source code is tested to validate expected behavior. (…) Testing is often performed by the programmer who writes and modifies the code under test. Unit testing may be viewed as part of the process of writing code.
Unit tests SHOULD BE QUICK TO RUN! Ideally, for a single test you want < 10s.
glm(), etc){devtools} and {usethis} simplify the process of development.If you can write a function, you can write a package (the rest is organization!).
{testthat}.{devtools}.DESCRIPTION (file)
NAMESPACE (file)
R/ (directory)
tests/ (directory)
man/ (directory)
vignettes/ (directory)
data/ (directory)
Let’s disentangle the previous slide:
DESCRIPTION (file): metadata file describing our package.NAMESPACE (file): which functions we want our users to have access to.R/ (directory): R functions.tests/ (directory): unit tests (!).man/ (directory): auto-generated documentation.vignettes/ (directory): authored long-form documentation/tutorials.data/ (directory): relevant datasetsWithin tests we have files that are used for testing. An example test file may look something like this:
test-arithmetic.R
We use the expect_* family of functions to define our tests. This layout is the backbone of testing in R.
expect_error, expect_warning to check for runtime problems.R packages with {testthat}.testthat} R package.devtools} R package.usethis} R package.Thanks for listening!