svUnit (version 0.7-12)

check: SciViews R Unit assertions (check functions)

Description

These functions define the assertions in test functions. They are designed to check the result of some test calculation.

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 = getOption("svUnit.silentException"))
DEACTIVATED(msg = "")

Arguments

current

an object created for comparison (not an S4 class object).

target

a target object as reference for comparison.

msg

an optional (short!) message to document a test. This message is stored in the log and printed in front of each test report.

tolerance

numeric >= 0. A numeric check does not fail if differences are smaller than `tolerance'.

checkNames

flag, if FALSE the names attributes are set to NULL for both current and target before performing the check.

expr

syntactically valid R expression which can be evaluated and must return a logical vector (TRUE|FALSE). A named expression is also allowed but the name is disregarded. In checkException(), expr is supposed to generate an error to pass the test.

silent

flag passed on to try, which determines if the error message generated by the checked function is displayed at the R console. By default, it is FALSE.

optional arguments passed to all.equal() or all.equal.numeric().

Value

TRUE if the test succeeds, FALSE if it fails, possibly with a 'result' attribute containing more information about the problem. This is very different from corresponding functions in 'RUnit' that stop with an error in case of test failure. Consequently, current functions do not require the complex evaluation framework designed in 'RUnit' for that reason.

Details

These check functions are equivalent to various methods of the class junit.framework.Assert of Java junit framework. They should be code-compatible with functions of same name in 'RUnit' 0.4.17, except for checkTrue() that is vectorized here, but accept only a scalar result in 'RUnit'. For scalar test, the behaviour of the function is the same in both packages.

See svTest() for examples of utilisation of these functions in actual test cases attached to R objects.

See also the note about S4 objects in the checkTrue() online help of the 'RUnit' package.

See Also

svTest, Log, guiTestReport, checkTrue

Examples

Run this code
# NOT RUN {
clearLog()			# Clear the svUnit log

## All these tests are correct
(checkEquals(c("A", "B", "C"), LETTERS[1:3]))
(checkEqualsNumeric(1:10, seq(1, 10)))
(checkIdentical(iris[1:50, ], iris[iris$Species == "setosa",]))
(checkTrue(1 < 2))
(checkException(log("a")))
Log()	# See what's recorded in the log

## ... but these ones fail
(checkEquals("A", LETTERS[1:3]))
(checkEqualsNumeric(2:11, seq(1, 10)))
(checkIdentical(iris[1:49, ], iris[iris$Species == "setosa",]))
(checkTrue(1 > 2))
(checkException(log(1)))
Log()	# See what's recorded in the log

## Create a test function and run it
foo <- function(x, y = 2) return(x * y)
test(foo) <- function () {
    #DEACTIVATED()
    checkEqualsNumeric(5, foo(2))
    checkEqualsNumeric(6, foo(2, 3))
    checkTrue(is.test(foo))
    checkTrue(is.test(test(foo)))
    checkIdentical(test(foo), attr(foo, "test"))
    checkException(foo("b"))
	checkException(foo(2, "a"))
}
(runTest(foo))

## Of course, everything is recorded in the log
Log()

clearLog()
# }

Run the code above in your browser using DataCamp Workspace