SAFEHR Learning session
2025-04-07
Record the expected behaviour of your code
No one likes to re-fix the same bug 5 times
Tests as documentation
Though not a valid replacement for actual documentation!
External APIs
Behaviour
Not every single function needs a test⦠but
A bug fix that doesnβt include an additional test to catch the bug is not an actual fix!
Strict TDD may often be overkill, but thinking about how you will test helps with design
Especially useful for bug fixing
when is my bug actually fixed?
testthat
packagetest-
<filename>.R
file with a test-<filename>.R
test_that()
blockstestthat
package: an exampleBasic structure of a test
testthat
packageexpect_*()
functions
helper.R
or helper-*.R
filessetup.R
skip_*()
helper functionswithr::local_tempfile()
or withr::local_tempdir()
to create temporary self-deleting filesrOpenSci | An Example of the DRY/DAMP Principles for Package Tests
test_object
is always defined, for every testtest_object
is only created in the tests where itβs neededUnit testing in R | https://milanmlft.github.io/talks/