Lesson 15 of 27
In Progress

Unit tests for R

For this section of the course, you’ll need the materials in this GitHub repo. You can download or clone them. After you download or clone the materials from the GitHub repo for this section of the course, open the entire project by clicking 04_avalanchr.Rproj.

Your Turn 1

Working in the exercises.Rmd file:

  • Use use_test() to create a new file. Call it “resident_connection”
  • Change the test description (the first argument of test_that()) to “connection is returning valid data”
  • In the test_that() function, remove the default expectations. Replace them with this code
  • Re-load your package.
  • Press the “Run tests” button in RStudio (above the script pane) or run test_file(“tests/testthat/test-resident_connection.R”) in the console.
Learn More

To learn about the testthat package, check out its website.

The R Packages book has a good chapter on testing.

Shannon Pileggi has also written a blog post called Getting started with unit testing in R.

Have any questions? Put them below and we will help you out!