makeTestListFromExamples {svUnit} | R Documentation |
Create, attach to and manipulate test functions in R objects
Description
Test functions are functions without arguments with class 'svTest' containing
one or more assertions using checkxxx()
functions. They can be
attached to any object as a 'test' attribute. They can also be transferred
into a more formal test unit file on disk by applying the makeUnit()
method.
Usage
makeTestListFromExamples(packageName, manFilesDir, skipFailing = FALSE)
svTest(testFun)
## S3 method for class 'svTest'
print(x, ...)
as.svTest(x)
is.svTest(x)
is.test(x)
test(x)
test(x) <- value
makeUnit(x, ...)
## Default S3 method:
makeUnit(
x,
name = make.names(deparse(substitute(x))),
dir = tempdir(),
objfile = "",
codeSetUp = NULL,
codeTearDown = NULL,
...
)
## S3 method for class 'svTest'
makeUnit(
x,
name = make.names(deparse(substitute(x))),
dir = tempdir(),
objfile = "",
codeSetUp = NULL,
codeTearDown = NULL,
...
)
runTest(x, ...)
## Default S3 method:
runTest(
x,
name = deparse(substitute(x)),
objfile = "",
tag = "",
msg = "",
...
)
## S3 method for class 'list'
runTest(x, ...)
## S3 method for class 'svTest'
runTest(
x,
name = deparse(substitute(x)),
objfile = "",
tag = "",
msg = "",
...
)
Arguments
packageName |
A character string identifying the package from which to extract examples. |
manFilesDir |
A character string identifying the directory holding the manual pages and examples. |
skipFailing |
A logical indicating whether missing or failing
documentation examples should be marked as |
testFun |
A function without arguments defining assertions (using
|
x |
Any kind of object. |
... |
Further arguments to the method (not used yet). |
value |
The tests to place in the object (as 'test' attribute); could be
a 'svTest' object, or a function without arguments with
assertions ( |
name |
The name of a test. |
dir |
The directory where to create the test unit file. |
objfile |
The path to the file containing the original source code of the object being tested. This argument is used to bring a context for a test and allow a GUI to automatically open the source file for edition when the user clicks on a test that failed or raised an error. |
codeSetUp |
An expression with some code you want to add to the
|
codeTearDown |
An expression with some code you want to add to the
|
tag |
A tag is a character string identifying a location in source code
files (either a test unit file, or the original source code of the tested
objects defined in |
msg |
A message you want to associate with this test run. |
Value
A 'svTest' object for svTest()
, as.svTest()
and test()
. Function
is.svTest()
returns TRUE
if 'x' is a 'svTest' object, and is.test()
does the same but also looks in the 'test' attribute if the class of 'x' is
not 'svTest' and returns TRUE
if it finds something there.
makeUnit()
takes an object, extract its test function and write it in a
sourceable test unit on the disk (it should be compatible with 'RUnit' test
unit files too).
runTest()
returns invisibly a 'svTestData' object with all results after running specified tests.
Author(s)
Philippe Grosjean
See Also
svSuite()
, is.svTestData()
, Log()
, checkEquals()
Examples
clearLog() # Clear the log file
foo <- function(x, y = 2)
return(x * y)
is.test(foo) # No
# Create test cases for this function
test(foo) <- function() {
checkEqualsNumeric(4, foo(2))
checkEqualsNumeric(6, foo(2, 3))
checkTrue(is.test(foo))
checkTrue(is.test(test(foo)))
checkIdentical(attr(foo, "test"), test(foo))
checkException(foo(2, "aa"))
checkException(foo("bb"))
}
is.test(foo) # Yes
## Not run:
# Create a test unit on disk and view it
unit <- makeUnit(foo)
file.show(unit, delete.file = TRUE)
## End(Not run)
# Run the test
(runTest(foo))
# Same as...
bar <- test(foo)
(runTest(bar))
# How fast can we run 100 times such kind of tests (700 test in total)?
# (just an indication because in real situation with test unit files, we
# have also the time required to source the units!)
system.time(for (i in 1:100) runTest(foo))[3]
is.svTest(test(foo)) # Yes, of course!
# When an object without associated test is passed to runTest(),
# a simple test containing only a DEACTIVATED entry is build
x <- 1:10
summary(runTest(x))
summary(Log())
rm(foo, bar, x)