Create, attach to and manipulate test functions in R objects

Share:

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
svTest(testFun)
as.svTest(x)
is.svTest(x)

test(x)
test(x) <- value
is.test(x)

## S3 method for class 'svTest'
print(x, ...)

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 'svTest'
runTest(x, name = deparse(substitute(x)), objfile = "",
	tag = "", msg = "", ...)
## S3 method for class 'list'
runTest(x, ...)

makeTestListFromExamples(packageName, manFilesDir, skipFailing=FALSE)

Arguments

testFun

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

x

any kind of object.

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.

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'.

...

further arguments to the method (not used yet).

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 <phgrosjean@sciviews.org>

See Also

svSuite, is.svTestData, check, Log

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)