How can I automatically generate test cases in RUnit?
For example, let's say I have a simple sum () function:
sum <- function(x, y) { return (x + y) }
I would like to test this feature on a number of different test cases:
test_cases <- c( c(2, 2, 4), c(3, 3, 6), c(0, 0, 0), c(-1, 2, 1) )
The first two elements of each vector are x and y, and the third is the expected result of the sum (x, y) function.
In python, I can easily write a function that generates a test case for each of the elements in test_cases, but I donβt know how to implement it in R. I looked at the RUnit and testthat documentation, but there is nothing like it. What is the best solution here?
Here is how I could write it in python (using nosetest to run a test module):
for triplet in test_cases: yield test_triplet(triplet) def test_triplet(triplet): assert(sum(triplet[0], triplet[1]) == triplet[2])