svTest: Create, attach to and manipulate test functions in R objects

Description Usage Arguments Value Author(s) See Also Examples

View source: R/svTest.R

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 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.

Author(s)

Philippe Grosjean

See Also

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

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
44
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)

svUnit documentation built on April 19, 2021, 9:06 a.m.