Description Usage Arguments Value Examples
Call the function functionName
with the arguments contained in
testdata
and compare the results with the results in testdata
for identity.
1 2 | test_function(functionName, testdata = loadArgs(functionName,
file.path(tempdir(), "test")), dbg = TRUE)
|
functionName |
Name of the function to test. It must be callable, i.e. either defined in the global environment or on the search path. |
testdata |
List of lists containing function arguments (in elemenet
|
dbg |
if |
TRUE
If the function functionName
is able to reproduce
the same results as given in the result
elements in testdata
for all the argument combinations given in the args
elements in
testdata
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Define a function using saveArgs() to save arguments and result
squareSum <- function(a, b) {
result <- a * a + b * b
saveArgs("squareSum", args = list(a = a, b = b), result = result)
result
}
# Set global variable TESTMODE to "activate" saveArgs() in squareSum()
TESTMODE <- TRUE
# Call the function with different arguments
squareSum(1, 2)
squareSum(2, 3)
squareSum(-1, -2)
# The arguments and function results were saved here:
dir(file.path(tempdir(), "test"))
# Write a new (wrong) version of the function
squareSum.new <- function(a, b) {
a * a - b * b
}
# Check if it returns the same results
test_function("squareSum.new", loadArgs("squareSum"))
# If no test data are given, loadArgs is called on the function to test,
# i.e. testing squareSum on the test data created by the same function will
# return TRUE if the function did not change in the meanwhile.
test_function("squareSum")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.