R/myfunc.r
<- function(a, b) {
myfunc <- a + b
c return(c)
}
Matthew DeHaven
March 31, 2024
There is a great chapter on unit tests in R Packages (2e).
Your last assignment had you create a package and host in on Github.
This example will show how we can add unit tests to a package.
Open a new folder in VS Code (or clone an empty repository from Github)
Make sure you have devtools
and usethis
installed
Run usethis::create_package(".")
in the terminal to convert the project to a package
testthat
testthat
to your packageCreate a new R script called “myfunc.r” in the “R/” folder
Add the following code
This is equivalent to just
Now we want to actually test our function.
Since we are within a pacakge, we can run any of the following to run our tests:
devtools::check()
which also checks a bunch of other thingsdevtools::test()
testthat::test_dir("test")