Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a testthat unit test for a function that returns a data frame

I am writing a script that ultimately returns a data frame. My question is around if there are any good practices on how to use a unit test package to make sure that the data frame that is returned is correct. (I'm a beginning R programmer, plus new to the concept of unit testing)

My script effectively looks like the following:

# initialize data frame
df.out <- data.frame(...)

# function set
function1 <- function(x) {...}
function2 <- function(x) {...}

# do something to this data frame
df.out$new.column <- function1(df.out)

# do something else
df.out$other.new.column <- function2(df.out)

# etc ....

... and I ultimately end up with a data frame with many new columns. However, what is the best approach to test that the data frame that is produced is what is anticipated, using unit tests?

So far I have created unit tests that check the results of each function, but I want to make sure that running all of these together produces what is intended. I've looked at Hadley Wickham's page on testing but can't see anything obvious regarding what to do when returning data frames.

My thoughts to date are:

  • Create an expected data frame by hand
  • Check that the output equals this data frame, using expect_that or similar

Any thoughts / pointers on where to look for guidance? My Google-fu has let me down considerably on this one to date.

like image 926
Blue Otter Hat Avatar asked Mar 26 '15 15:03

Blue Otter Hat


People also ask

What is Testthat in R?

'testthat' is a testing framework for R that is easy to learn and use, and integrates with your existing 'workflow'. License MIT + file LICENSE. URL https://testthat.r-lib.org, https://github.com/r-lib/testthat.


1 Answers

Your intuition seems correct. Construct a data.frame manually based on the expected output of the function and then compare that against the function's output.

# manually created data
dat <- iris[1:5, c("Species", "Sepal.Length")]

# function
myfun <- function(row, col, data) {
    data[row, col]
}

# result of applying function
outdat <- myfun(1:5, c("Species", "Sepal.Length"), iris)

# two versions of the same test
expect_true(identical(dat, outdat))
expect_identical(dat, outdat)

If your data.frame may not be identical, you could also run tests in parts of the data.frame, including:

  • dim(outdat), to check if the size is correct
  • attributes(outdat) or attributes of columns
  • sapply(outdat, class), to check variable classes
  • summary statistics for variables, if applicable
  • and so forth
like image 191
Thomas Avatar answered Oct 16 '22 12:10

Thomas