Get startedGet started for free

Unit tests

1. Unit tests

We'll next discuss unit tests and why they are important to package development and maintenance. We'll explore how to create a test file and how the testthat package makes our lives easier when working with unit tests. We will save the actual writing of the components of a unit test until later in the chapter.

2. R package structure

We are now in the tests section of our R package structure.

3. Testing your code works

Throughout, we've talked about the need to build documentation to help users work with our package. It also helps us as a package author in developing new functionality and maintaining our package. We discussed the importance of examples testing the functionality of our package and the expected output. We need to manually check that all of the function examples in our package still work whenever we try to add in new functionality or fix a bug. This is error prone and hard to keep track of.

4. What are unit tests?

Unit testing provides an automated solution where all tests can be run at once, verifying individual components of an R package, checking code behaves as intended, and produces accurate results. Unit testing as a framework is built from many different unit tests put together. Each unit test checks for particular characteristics of a function working. A unit test itself does not, thus, check the functionality of the entire package, but when combined together it can do so in one swoop. These tests catch bugs and errors early, providing a safety net during code modification. Adopting a test-driven development approach helps us write clean, modular, and testable code, facilitating collaboration among developers. Unit testing maintains code quality, and improves software reliability.

5. Structuring unit testing in R

The usethis package can help us create the structure necessary for unit testing. We then can fill in the necessary files to create tests of our functions. The use_testthat function sets up the testing skeleton for us with the testthat R package. testthat allows for unit tests to be written in a standard process in line with other devtools and usethis functionality. It performs additional helpful tasks such as adding the testthat package to Suggests in the DESCRIPTION. use_testthat will also default to the most recent edition of testthat, in this case, three. As one can see from the output of use_testthat, it also creates a testthat-dot-R file which contains code to run our tests (that we will start building in the next video). The last piece of output is a suggestion to call the use_test function to start a testing file for a particular function. We'll do that next.

6. Creating test template files

To help us match the R files with their corresponding tests, we will use a standard convention. We have many different R files in our package so far. Two of them are temp_converter-dot-R and time_converter-dot-R. The files in the tests-slash-testthat folder have similar names except test-hyphen is added to the beginning of them. This prefix addition is best practice and also a requirement of testing script files.

7. A function for creating these files

The use_test function from the usethis package creates these files. Its argument is the function name. Its output tells us a template file for creating tests has been added. As a reminder, we save the actual writing of the components of a unit test until later in the chapter.

8. Let's practice!

Check out your understanding of testing so far before we get into writing expectations as the foundations of unit tests in the next video.