Learn R Programming

RUnit (version 0.4.21)

checkFuncs: RUnit check functions

Description

A set of functions used to check the results of some test calculation. If these functions are called within the RUnit framework, the results of the checks are stored and reported in the test protocol. checkEquals compares two R objects by invoking all.equal on the two objects. If the objects are not equal an error is generated and the failure is reported to the test logger such that it appears in the test protocol. checkEqualsNumeric works just like checkEquals except that it invokes all.equal.numeric instead of all.equal checkIdentical is a convenience wrapper around identical using the error loggin mechanism of RUnit. checkTrue uses the function identical to check if the expression provided as first argument evaluates to TRUE. If not, an error is generated and the failure is reported to the test logger such that it appears in the test protocol. checkException evaluates the passed expression and uses the try mechanism to check if the evaluation generates an error. If it does the test is OK. Otherwise an error is generated and the failure is reported to the test logger such that it appears in the test protocol. DEACTIVATED interrupts the test function and reports the test case as deactivated. In the test protocol deactivated test functions are listed separately. Test case deactivation can be useful in the case of major refactoring. Alternatively, test cases can be commented out completely but then it is easy to forget the test case altogether.

Usage

checkEquals(target, current, msg, tolerance = .Machine$double.eps^0.5, checkNames = TRUE, ...)
checkEqualsNumeric(target, current, msg, tolerance = .Machine$double.eps^0.5, ...)
checkIdentical(target, current, msg)
checkTrue(expr, msg)
checkException(expr, msg, silent=FALSE)
DEACTIVATED(msg)

Arguments

encoding

latin1

concept

RUnit

Details

The check functions are direct equivalents of the various methods of the class junit.framework.Assert of Javas junit framework which served as basis for the RUnit package.

For functions defined inside a package equipped with a namespace only exported functions can be accessed inside test cases directly. For functions not exported the only way to test them is to use the ':::' operator combined with the packagename as a prefix. Special care is required if test cases are written for S4 classes and methods. If a new class is defined inside a test case via a setClass call the class is added to the global class cache and thus available outside the test case. It will persist until explicitly removed via a removeClass call. Same applies for new method and generic definitions. Be sure to remove methods and classes in each test case they are defined after the checks have been performed. This is an advise gained from the cumbersome experience: not doing so leads to difficult to pin down error causes incurred from previously executed test cases. For a simple example see the provided test cases in examples/runitVirtualClassTest.r.

See Also

all.equal, all.equal.numeric and identical are the underlying comparison functions. try is used for error catching.

Examples

Run this code
checkTrue(1 < 2, "check1")     ## passes fine
## checkTrue(1 > 2, "check2")  ## appears as failure in the test protocol

v <- 1:3
w <- 1:3
checkEquals(v,w)          ## pasess fine
names(v) <- c("A", "B", "C")
## checkEquals(v,w)      ## fails because v and w have different names
checkEqualsNumeric(v,w)  ## passes fine because names are ignored


fun <- function(x) {
  if(x)
  {
   stop("stop conditions signaled")
  }
  return()
}

checkException(fun(TRUE))   ## passes fine
## checkException(fun(FALSE))  ## failure, because f raises no error
checkException(fun(TRUE), silent=TRUE)

## DEACTIVATED("here one can document on the reason for deactivation")

Run the code above in your browser using DataLab