These functions define the assertions in test functions. They are designed to check the result of some test calculation.
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 = "")
a target object as reference for comparison.
An object created for comparison (not an S4 class object).
An optional (short!) message to document a test. This message is stored in the log and printed in front of each test report.
numeric >= 0. A numeric check does not fail if differences are smaller than 'tolerance'.
Flag, if FALSE
the names attributes are set to NULL
for
both current
and target
before performing the check.
Optional arguments passed to all.equal()
or
all.equal.numeric()
.
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.
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
.
These function return 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.
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 behavior of the function is the same in both
packages.
See svTest()
for examples of use of these functions in
actual test cases attached to R objects.
See also the note about S4 objects in the RUnit::checkTrue()
online help of the
'RUnit' package.
svTest()
, Log()
, guiTestReport()
, RUnit::checkTrue
clearLog() # Clear the svUnit log
# All these tests are correct
(checkEquals(c("A", "B", "C"), LETTERS[1:3]))
#> [1] TRUE
(checkEqualsNumeric(1:10, seq(1, 10)))
#> [1] TRUE
(checkIdentical(iris[1:50, ], iris[iris$Species == "setosa",]))
#> [1] TRUE
(checkTrue(1 < 2))
#> [1] TRUE
(checkException(log("a")))
#> [1] TRUE
Log() # See what's recorded in the log
#> = A svUnit test suite run in less than 0.1 sec with:
#>
#> * eval ... OK
#>
#>
#> == eval run in less than 0.1 sec: OK
#>
#> //Pass: 5 Fail: 0 Errors: 0//
#>
# ... but these ones fail
(checkEquals("A", LETTERS[1:3]))
#> * : checkEquals("A", LETTERS[1:3]) run in less than 0.001 sec ... **FAILS**
#> Lengths (1, 3) differ (string compare on first 1)
#> chr [1:3] "A" "B" "C"
#> [1] FALSE
(checkEqualsNumeric(2:11, seq(1, 10)))
#> * : checkEqualsNumeric(2:11, seq(1, 10)) run in less than 0.001 sec ... **FAILS**
#> Mean relative difference: 0.1538462
#> int [1:10] 1 2 3 4 5 6 7 8 9 10
#> [1] FALSE
(checkIdentical(iris[1:49, ], iris[iris$Species == "setosa",]))
#> * : checkIdentical(iris[1:49, ], iris[iris$Species == "setosa", ]) run in less than 0.001 sec ... **FAILS**
#> 'data.frame': 50 obs. of 5 variables:
#> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#> [1] FALSE
(checkTrue(1 > 2))
#> * : checkTrue(1 > 2) run in less than 0.001 sec ... **FAILS**
#> logi FALSE
#> [1] FALSE
(checkException(log(1)))
#> * : checkException(log(1)) run in less than 0.001 sec ... **FAILS**
#> No exception generated!
#> [1] FALSE
Log() # See what's recorded in the log
#> = A svUnit test suite run in less than 0.1 sec with:
#>
#> * eval ... **FAILS**
#>
#>
#> == eval run in less than 0.1 sec: **FAILS**
#>
#> //Pass: 5 Fail: 5 Errors: 0//
#>
#> * : checkEquals("A", LETTERS[1:3]) run in less than 0.001 sec ... **FAILS**
#> Lengths (1, 3) differ (string compare on first 1)
#> chr [1:3] "A" "B" "C"
#>
#> * : checkEqualsNumeric(2:11, seq(1, 10)) run in less than 0.001 sec ... **FAILS**
#> Mean relative difference: 0.1538462
#> int [1:10] 1 2 3 4 5 6 7 8 9 10
#>
#> * : checkIdentical(iris[1:49, ], iris[iris$Species == "setosa", ]) run in less than 0.001 sec ... **FAILS**
#> 'data.frame': 50 obs. of 5 variables:
#> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#>
#> * : checkTrue(1 > 2) run in less than 0.001 sec ... **FAILS**
#> logi FALSE
#>
#> * : checkException(log(1)) run in less than 0.001 sec ... **FAILS**
#> No exception generated!
# 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))
#> * : checkEqualsNumeric(5, foo(2)) run in less than 0.001 sec ... **FAILS**
#> Mean relative difference: 0.2
#> num 4
#>
#> == test(foo) run in less than 0.1 sec: **FAILS**
#>
#> //Pass: 6 Fail: 1 Errors: 0//
#>
#> * : checkEqualsNumeric(5, foo(2)) run in less than 0.001 sec ... **FAILS**
#> Mean relative difference: 0.2
#> num 4
# Of course, everything is recorded in the log
Log()
#> = A svUnit test suite run in less than 0.1 sec with:
#>
#> * test(foo) ... **FAILS**
#> * eval ... **FAILS**
#>
#>
#> == test(foo) run in less than 0.1 sec: **FAILS**
#>
#> //Pass: 6 Fail: 1 Errors: 0//
#>
#> * : checkEqualsNumeric(5, foo(2)) run in less than 0.001 sec ... **FAILS**
#> Mean relative difference: 0.2
#> num 4
#>
#> == eval run in less than 0.1 sec: **FAILS**
#>
#> //Pass: 5 Fail: 5 Errors: 0//
#>
#> * : checkEquals("A", LETTERS[1:3]) run in less than 0.001 sec ... **FAILS**
#> Lengths (1, 3) differ (string compare on first 1)
#> chr [1:3] "A" "B" "C"
#>
#> * : checkEqualsNumeric(2:11, seq(1, 10)) run in less than 0.001 sec ... **FAILS**
#> Mean relative difference: 0.1538462
#> int [1:10] 1 2 3 4 5 6 7 8 9 10
#>
#> * : checkIdentical(iris[1:49, ], iris[iris$Species == "setosa", ]) run in less than 0.001 sec ... **FAILS**
#> 'data.frame': 50 obs. of 5 variables:
#> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#>
#> * : checkTrue(1 > 2) run in less than 0.001 sec ... **FAILS**
#> logi FALSE
#>
#> * : checkException(log(1)) run in less than 0.001 sec ... **FAILS**
#> No exception generated!
clearLog()