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 function on a series 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 output of the sum(x,y) function.
In python I can easily write a function that generate a test case for each of the elements in test_cases, but I don't know how to implement it in R. I have looked at the RUnit and testthat documentation, but there is nothing similar. What is the best solution here?
This is how I would write it in python (using nosetest to launch the test unit):
for triplet in test_cases:
yield test_triplet(triplet)
def test_triplet(triplet):
assert(sum(triplet[0], triplet[1]) == triplet[2])
Many open-source tools that automatically generate unit tests look valuable at first because your coverage goes up very quickly. It’s in the maintenance that the real problems occur. Often times during development, developers will put in extra effort to fine-tune the auto-generated assertions to create what they think is a clean test suite.
Thus, we created two generation modes: safe (i.e., only GET operations are considered to generate test cases), and unsafe (i.e., all operations are considered to generate test cases). Figure 5: TestSuite model representing nominal and faulty test cases for the Petstore API.
You’ll see your extensions in Chrome as buttons on the toolbar. To record the test case, click on the TestCase Studio icon on the toolbar. Now keep performing your steps, it will automatically record all the user actions. You can save the recorded steps by clicking on the Download Test Case button.
Leverage automatic JUnit code coverage test case generation to quickly build and expand your tests to get meaningful, maintainable complete code coverage. Unit test coverage is a great way to make sure you’re measuring everything correctly.
# You simply take advantage of R's vector orientation.
test_cases <- matrix(c(2, 2, 4,
3, 3, 6,
0, 0, 0,
-1, 2, 1), ncol = 3, byrow = TRUE)
my_sum <- function(x, y) { x + y}
## testthat
library(testthat)
expect_equal(my_sum(test_cases[ , 1], test_cases[ , 2]), test_cases[ , 3])
## RUnit
library(RUnit)
test_my_sum <- function() {
checkEquals(my_sum(test_cases[ , 1], test_cases[ , 2]), test_cases[ , 3])
}
sapply could be useful
Sum <- function(x, y) { # Sum is much better than sum,this avoids problems with sum base function
return (x + y)
}
test_cases <- matrix( c(2, 2, 4, # I think a matrix structure is better to handle this problem
3, 3, 6,
0, 0, 0,
-1, 2, 1), ncol=3, byrow=TRUE)
# Applying your function and comparing the result with the expected result.
sapply(1:nrow(test_cases), function(i) Sum(test_cases[i,1], test_cases[i,2]))==test_cases[,3]
TRUE TRUE TRUE TRUE # indicates the result is as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With