| Title: | Test whether Different Functions Return the Same |
|---|---|
| Description: | Functions to test whether different functions return the same results. I use these functions to test whether a function still returns the same result after cleaning code. |
| Authors: | Hauke Sonnenberg [aut, cre] (ORCID: <https://orcid.org/0000-0001-9134-2871>), Kompetenzzentrum Wasser Berlin gGmbH [cph] |
| Maintainer: | Hauke Sonnenberg <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-05-19 06:39:27 UTC |
| Source: | https://github.com/KWB-R/kwb.test |
Create test files for each source file containing one
test_that call for each function in the package
create_test_files(package_dir = getwd(), target_dir = NULL, file_per_function = TRUE, full = FALSE, dbg = TRUE)create_test_files(package_dir = getwd(), target_dir = NULL, file_per_function = TRUE, full = FALSE, dbg = TRUE)
package_dir |
path to package directory in which to create the test files |
target_dir |
directory in which to create the test files. Defaults to
|
file_per_function |
if |
full |
if |
dbg |
if |
Existing test files will not be overwritten.
Read the function arguments and function results that were stored in RData
files (in objects args and result, respectively) by a previous
call of saveArgs.
loadArgs(functionName = NULL, data.dir = file.path(tempdir(), "test"))loadArgs(functionName = NULL, data.dir = file.path(tempdir(), "test"))
functionName |
Name of the function to load arguments and results for.
The name is used to create a search pattern for RData files in
|
data.dir |
Directory in which to look for RData files matching
|
list with as many items as there were files args_<functionName>_*
in the directory given in data.dir. Each list element has two
components: args containing the arguments that were given to
the function and result containing what the function returned.
# Define a function that stores its arguments and result with saveArgs double <- function(x) { result <- 2 * x saveArgs("double", args = list(x = x), result = result) result } # Set global variable TESTMODE to "activate" saveArgs() in double() TESTMODE <- TRUE # Call the function a couple of times double(4) double(-99) double(1:10) # Load what was stored behind the scenes testdata <- loadArgs("double") # "Deactivate" saveArgs() in double() TESTMODE <- FALSE # Rerun the function with the stored arguments results <- lapply(testdata, function(x) do.call("double", x$args)) # Compare the new with the old results identical(results, lapply(testdata, "[[", "result"))# Define a function that stores its arguments and result with saveArgs double <- function(x) { result <- 2 * x saveArgs("double", args = list(x = x), result = result) result } # Set global variable TESTMODE to "activate" saveArgs() in double() TESTMODE <- TRUE # Call the function a couple of times double(4) double(-99) double(1:10) # Load what was stored behind the scenes testdata <- loadArgs("double") # "Deactivate" saveArgs() in double() TESTMODE <- FALSE # Rerun the function with the stored arguments results <- lapply(testdata, function(x) do.call("double", x$args)) # Compare the new with the old results identical(results, lapply(testdata, "[[", "result"))
Print a test with its result as a message and return the message as a character string
printTestMessage(testexpression, testresult, newline = TRUE)printTestMessage(testexpression, testresult, newline = TRUE)
testexpression |
text description of what was tested |
testresult |
boolean result (of length one) of the test |
newline |
if |
the message that was shown as a character string
printTestMessage("apple == apple", 1 == 1) printTestMessage("apple == pear", 1 == 2)printTestMessage("apple == apple", 1 == 1) printTestMessage("apple == pear", 1 == 2)
Save the list of named arguments given in ... to an RData file
args_<functionName>_<hhmmss>_<no>.RData in the directory given in
targetdir. This function can be used to log the inputs given to a
function together with the result returned by the function.
test_function can then be used to check whether another version
of the function (e.g. obtained by code cleaning) can reproduce the stored
results from the stored arguments. Check out the example on the help page for
test_function.
saveArgs(functionName, ..., targetdir = kwb.utils::createDirectory(file.path(tempdir(), "test")))saveArgs(functionName, ..., targetdir = kwb.utils::createDirectory(file.path(tempdir(), "test")))
functionName |
name of the function to which the arguments to be saved belong. It will be used to generate a file name for the RData file. |
... |
named arguments representing the arguments that have been given
to the function |
targetdir |
directory in which to store the objects given in |
path to the file written (invisibly)
Call the function functionName with the arguments contained in
testdata and compare the results with the results in testdata
for identity.
test_function(functionName, testdata = loadArgs(functionName, file.path(tempdir(), "test")), dbg = TRUE)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.
# 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")# 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")
For all columns in the first data frame, check if the second data frame has identical values in columns of the same name
testColumnwiseIdentity(...)testColumnwiseIdentity(...)
... |
two data frames given as named arguments. The argument names will appear in the output. By doing so you can give a longer expression that returns a data frame a short name 'on-the-fly'. |
# Compare two identical data frames. Give them short names data.1 and data.2 testColumnwiseIdentity(data.1 = (x <- data.frame(a = 1:2, b = 2:3)), data.2 = x) # Compare two data frames differing in one column testColumnwiseIdentity(A = data.frame(x = 1:2, y = 2:3), B = data.frame(x = 1:2, y = 3:4))# Compare two identical data frames. Give them short names data.1 and data.2 testColumnwiseIdentity(data.1 = (x <- data.frame(a = 1:2, b = 2:3)), data.2 = x) # Compare two data frames differing in one column testColumnwiseIdentity(A = data.frame(x = 1:2, y = 2:3), B = data.frame(x = 1:2, y = 3:4))