Definition and execution of RUnit test suites.
runTestSuite is the central function of the RUnit package.
Given one or more test suites it identifies and sources specified test
code files one after another and executes all specified test functions
defined therein. This is done sequentially for suites, test code files
and test functions. During the execution information about the test function calls including the
possible occurrence of failures or errors is recorded and returned at the
end of the test run. The return object can then be used to create a
test protocol of various formats.
runTestFile is just a convenience function for executing the
tests in a single test file.
defineTestSuite is a helper function to define a test
suite. See below for a precise definition of a test suite.
isValidTestSuite checks if an object defines a valid test suite.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
defineTestSuite(name, dirs, testFileRegexp = "^runit.+\\.[rR]$", testFuncRegexp = "^test.+", rngKind = "Marsaglia-Multicarry", rngNormalKind = "Kinderman-Ramage") unitTestSuite(name, dirs, rngKind = "Marsaglia-Multicarry", rngNormalKind = "Kinderman-Ramage", testFileRegexp = "^runit.+\\.[rR]$", testFuncRegexp = "^test.+", ...) isValidTestSuite(self) runTestSuite(testSuite, ...) runTestFile(absFileName, useOwnErrorHandler = TRUE, testFuncRegexp = "^test.+", rngKind = "Marsaglia-Multicarry", rngNormalKind = "Kinderman-Ramage", verbose = getOption("RUnit")$verbose)
The name of the test suite.
Vector of absolute directory names where to look for test files.
Regular expression for matching test files.
Regular expression for matching test functions.
name of an available RNG (see
name of a valid rnorm RNG version (see
A single object, of class RUnitTestSuite or similar.
A single object of class test suite or a list of test suite objects.
level of verbosity of output log messages, 0: omits begin/end comments for each test function. Queried from global options set for RUnit at package load.
Absolute file name of a test function.
passed to included functions or for later extensions.
The basic idea of the RUnit test framework is to declare a certain set of functions to be test functions and report the results of their execution. The test functions must not take any parameter nor return anything such that their execution can be automatised.
The specification which functions are taken as test functions is
contained in an object of class
RUnitTestSuite which is a list
with the following elements.
A simple character string. The name of a test suite is mainly used to create a well structure test protocol.
A character vector containing the absolute names of all directories where to look for test files.
A regular expression specifying the test files. All files in the test directories whose names match this regular expression are taken as test files. Order of file names will be alphabetical but depending on the used locale.
A regular expression specifying the test functions. All functions defined in the test files whose names match this regular expression are used as test functions. Order of test functions will be alphabetical.
After the RUnit framework has sequentially executed all test suites it returns all
data collected during the test run as an object of class
RUnitTestData. This is a (deeply nested)
list with one list element for each executed test suite. Each of these
executed test suite lists contains the following elements:
The number of test functions executed in the test suite.
The number of errors that occurred during the execution.
The number of failures that occurred during the execution.
The test directories of the test suite.
The regular expression for identifying the test files of the test suite.
The regular expression for identifying the test functions of the test suite.
A list containing the results for each separate test file of the test suite.
sourceFileResults list just mentioned contains one element
for each specified test function in the source file. This element is a list with
the following entries:
Character string with one of
failuredescribing the outcome of the test function.
the error message in case of an error or failure and
NULLfor a successfully executed test function.
The duration (measured in seconds) of the successful execution of a test function and
NULLin the case of an error or failure.
The full trace back as a character vector in the case of an error and
To further control test case execution it is possible to define two
.tearDown in each
.setUp() is executed directly before and
.tearDown() directly after each test function execution.
Quite often, it is useful to base test cases on random numbers. To
make this procedure reproducible, the function
sets the random number generator to the default setting
normal.kind="Kinderman-Ramage") before sourcing each test file
(note that this default has been chosen due to historical reasons and
differs from the current R default). This default can be overwritten
by configuring the random number generator at the beginning of a test
file. This setting, however, is valid only inside its own source file
and gets overwritten when the next test file is sourced.
runTestFile both return an object of
defineTestSuite returns an object of class
Thomas König, Klaus Jünemann & Matthias Burger
checkTrue and friends for writing test cases.
for printing the test protocol. See RUnit-options for global
options controlling log out.
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
## run some test suite myTestSuite <- defineTestSuite("RUnit Example", system.file("examples", package = "rtest"), testFileRegexp = "correctTestCase.r") testResult <- runTestSuite(myTestSuite) ## same but without the logger being involved ## source(file.path(system.file("examples", package = "RUnit"), ## "correctTestCase.r")) ## test.correctTestCase() ## prints detailed text protocol ## to standard out: printTextProtocol(testResult, showDetails = TRUE) ## use current default RNGs myTestSuite1 <- defineTestSuite("RUnit Example", system.file("examples", package = "rtest"), testFileRegexp = "correctTestCase.r", rngKind = "Mersenne-Twister", rngNormalKind = "Inversion") testResult1 <- runTestSuite(myTestSuite) ## for single test files, e.g. outside a package context testResult2 <- runTestFile(file.path(system.file("examples", package = "rtest"), "correctTestCase.r")) printTextProtocol(testResult2, showDetails = TRUE)
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.