![](https://static.isu.pub/fe/default-story-images/news.jpg?width=720&quality=85%2C50)
2 minute read
Collaborating on GitHub������������������������������������������������������������������������������������������������
Chapter 12 ■ testing and paCkage CheCking
Take for example the shapes exercise we had earlier, where you had to write functions for computing the area and circumference of different shapes. I have written a version where I specify rectangles by width and height. 1 A test of the two functions could then look like this:
Advertisement
area <- function(x) UseMethod("area") circumference <- function(x) UseMethod("circumference")
rectangle <- function(width, height) { structure(list(width = width, height = height), class = c("rectangle", "shape"))
} area.rectangle <- function(x) x$height * x$width circumference.rectangle <- function(x) 2 * x$height + 2 * x$width r <- rectangle(width = 2, height = 4) area(r) ## [1] 8 circumference(r) ## [1] 12
The area is 2×4 and the circumference is 2×2+2×4, so this looks fine. But I am testing the code by calling the functions and looking at the printed output. I don’t want to test the functions that way forever—I cannot automate my testing this way because I then have to sit and look at the output of my tests. But they are okay tests. I just need to automate them.
Automating Testing
All it takes to automate the test is to check the result of the functions in code rather than looking at it, so code that resembles the following would be an automated test:
r <- rectangle(width = 2, height = 4) if (area(r) != 2*4) { stop("Area not computed correctly!")
} if (circumference(r) != 2*2 + 2*4) { stop("Circumference not computed correctly!")
It is a little more code, yes, but it is essentially the same test, and this is something I can run automatically later on. If it doesn’t complain about an error, then the tests are passed, and all is good.
You can write your own test this way. Put them in a directory called tests/ (which is where R expect tests to live) and then run these tests whenever you want to check the status of your code, i.e., whenever you make modifications to it.
Scripts in the tests/ directory will also be automatically run whenever you do a consistency check of the package (something we return to shortly). That is what happens when you click Check in the Build tab on the right in RStudio or select Check Package in the Build menu, but it does a lot more than just run tests so it is not the most efficient way of running the tests.
There are some frameworks for formalizing this type of testing in R. I use a framework called testthat. Using this framework it is easy to run tests (without the full package check) and easy to write tests in a more structured manner—of course at the cost of having a bit more code to write for each test.
1I know that a rectangle doesn’t have to have sides parallel with those two dimensions, but there is no need to make the example more complicated than it has to be.
282