svUnit (version 1.0.3)

makeTestListFromExamples: 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 svTest print(x, ...)

as.svTest(x)

is.svTest(x)

is.test(x)

test(x)

test(x) <- value

makeUnit(x, ...)

# S3 method for default makeUnit( x, name = make.names(deparse(substitute(x))), dir = tempdir(), objfile = "", codeSetUp = NULL, codeTearDown = NULL, ... )

# S3 method for svTest makeUnit( x, name = make.names(deparse(substitute(x))), dir = tempdir(), objfile = "", codeSetUp = NULL, codeTearDown = NULL, ... )

runTest(x, ...)

# S3 method for default runTest( x, name = deparse(substitute(x)), objfile = "", tag = "", msg = "", ... )

# S3 method for list runTest(x, ...)

# S3 method for 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 skipped instead of as failure.

testFun

A function without arguments defining assertions (using checkxxx() functions) for tests to be transformed into a 'svTest' object.

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 (checkxxx() functions).

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 .setUp() function in your unit file (this function is executed before each test.

codeTearDown

An expression with some code you want to add to the .tearDown() function in your unit file (this function is executed after each test.

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 objfile =. This character string will be searched by the text editor for easy location of the cursor near the corresponding test command, or near the location in the original object that is concerned by this test. Use any string you want to uniquely identify your tag, both in your files, and in this argument.

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.

See Also

svSuite(), is.svTestData(), Log(), checkEquals()

Examples

Run this code
# NOT RUN {
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)
# }
# 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)
# }

Run the code above in your browser using DataCamp Workspace