R Unit Test Framework

R functions implementing a standard Unit Testing framework, with additional code inspection and report generation tools.

CRAN_Status_Badge CRAN Downloads Travis-CI Build Status

RUnit is a testing package for R code, inspired by the xUnit family of testing tools.

Originally implemented by Thomas Koenig, Klaus Juenemann, and Matthias Burger, this package has served the R community for over a decade.

Since RUnit is no longer actively developed, I provide maintenance of this package mostly to support older projects that still rely on RUnit.

Using RUnit

To make RUnit work with R CMD check, create a following file in the tests subdirectory. This would run the actual tests stored in the packages inst/tests subdirectory.

# Our package. Used for the test suite name
pkgname <- "your package name"
require(pkgname, quietly=TRUE, character.only=TRUE) || stop("package '", pkgname, "' not found")

# How to determine which files to load (have to start with test_ and end with .R)
pattern <- "^test_.*\\.R$"

# Which functions to run. Have to start with 'test.'
testFunctionRegexp = "^test.+"

# Path to the unit tests folder in the package
dir <- system.file(subdir, package=pkgname)

# Define RUnit test suite
suite <- defineTestSuite(name=paste(pkgname, "RUnit Tests"),
                         testFuncRegexp = testFunctionRegexp,

# Run tests
result <- runTestSuite(suite)

# Display result tests on the console

# Write results in JUnit-like xml format
printJUnitProtocol(result, fileName="junit.xml")

A typical unit test would then live for example in inst/tests/test.operations.R. Each such file can contain multiple functions like so:

test.additionWorks <- function() {
  checkEquals(2, 1+1, "one plus one must be two")

test.divisionByZeroFails <- function() {
  checkException(1/0, "division by zero is expected to fail")


    Dear Emacs, please make this -*-Text-*- mode!
    *       RUnit

Changes in RUnit 0.4.32 o runTestSuite and runTestFile takes additional gcBeforeTest parameter (default FALSE). This disables running garbage collector before timing the test. This setting speeds up the test suite at the cost of making individual test timing less reliable. When trying to optimize tests for speed, set gcBeforeTest to TRUE for more reliable timing information. o added .Rinstignore to cut down on warnings when building the package o removed Biobase specific tests, replaced with direct S4 class creation o fixed CRAN URLs in README.md

Changes in RUnit 0.4.31 o checkEquals and others output optional message on separate line

Changes in RUnit 0.4.30 o printJUnitProtocol added for JUnit-style output

Changes in RUnit 0.4.29 o changed maintainer to Roman Zenka o .testLogger global variable now stored in package environment RUnitEnv o added imports of graphics package to NAMESPACE

Changes in RUnit 0.4.26 o isValidTestSuite: fixed insufficient if expression handling, reported by Rich Calaway; extended validity checks

Changes in RUnit 0.4.25 o enable redirection of error and log messages to file, controlled via new global option 'outfile', (following a suggestion by Seth Falcon)

Changes in RUnit 0.4.24 o added RUnit specific options 'verbose' and 'silent' to global options list to allow control of test log output o moved unit tests from tests/ to inst/unitTests o added Makefile for executing unit tests (using R wiki example)

Changes in RUnit 0.4.23 o vignette: fixed function name in example code, reported by Blair Christian o init .testLogger to avoid R CMD check NOTE messages o allow verbosity of console output log to be controlled: added 'verbose' argument to runTestFile and runTestSuite (following a suggestion by Seth Falcon) o test logger object declared as S3 class 'TestLogger'

Changes in RUnit 0.4.22 o clarified applicable license: GPL 2 o defineTestSuite: gained some argument checks o isValidTestSuite: check for empty name o includeTracker: fix <- if handling

Changes in RUnit 0.4.21 o documentation issues corrected, identified by new R 2.9.0 devel Rd parser

Changes in RUnit 0.4.20 o test protocol generation on Mac OS X failed due to incorrect code to identify 'gcc' version
o Rd documentation updated

Changes in RUnit 0.4.19 o test protocol now states check number per test case o changed check for object class to is() to allow derived class objects to pass (suggested by Philippe Grosjean) o removed start up message

Changes in RUnit 0.4.18 o seq_along introduced instead of seq( ) for efficiency and R version dependency set to 2.4.0 when seq_along was introduced o some small changes to avoid warnings with options(warnPartialMatchArgs=TRUE)

Changes in RUnit 0.4.17 o corrected documentation example code

Changes in RUnit 0.4.16 o changed the environment test code files are evaluated, now a new environment outside the RUnit namespace is utilized, allowing e.g. setClass calls without specifying where=.GlobalEnv o updated documentation to use encoding latin1 o use LazyLoad: yes instead of SaveImage:yes (to be deprecated for R 2.6.0) o internal error handler rewritten to be more failure robust o added new test cases for .setUp and .tearDown, extended tests to cover S4 class and method behaviour in check* functions o example on S4 virtual class testing added o utility function to compare to RUnitTestResult objects added: concept idea for comparing and optimizing test suite performance (share/R/checkCode.r)

Changes in RUnit 0.4.15 o compatibility to R 1.9.0 as declared in DESCRIPTION: removed calls to isTRUE as this was introduced only in R 2.1.0, replaced where needed by identical(TRUE, x) o fixed printHTMLProtocol: on Windows Makeconf does not exist so CC compiler used by R cannot be queried this way (reported by Tobias Verbeke)

Changes in RUnit 0.4.14 o stated all package dependencies in DESCRIPTION, required for R 2.4.0 compatibility

Changes in RUnit 0.4.13 o allow the RNG to be set by the user via new arguments 'rngKind' and 'rngNormalKind' to functions defineTestSuite and runTestFile (patch by Seth Falcon) o fixed exit status: RNG kind was left changed after runTestSuite execution in user workspace o email contact address modified

Changes in RUnit 0.4.12 o allow *.R test case file extension (suggested by Gregor Gorjanc) o fixed code typo in example vignette (spotted by Gregor Gorjanc)

Changes in RUnit 0.4.11 o checkException: argument silent added to allow to suppress error messages emitted by the failing function o inspect: added argument track, which has to be provided at each invocation to avoid recursive default argument reference call errors (experimental: subject to change if this leads to new incompatibilities)

Changes in RUnit 0.4.9 o checkEquals has new compatibility argument checkNames

Changes in RUnit 0.4.8 o checkIdentical added, to allow to check for exact identity

Changes in RUnit 0.4.7 o update for checkEqualsNumeric to be compatible with R 2.3.0

Changes in RUnit 0.4.5 o improvements to error detection in runTestSuite

Changes in RUnit 0.4.4 o changed maintainer

Changes in RUnit 0.4.2 o checkTrue: corrected handling of named logical arguments

Changes in RUnit 0.4.1 o printHTMLProtocol has new parameter 'testFileToLinkMap' to allow to provide a function to add dynamically generated URLs for each test case file, e.g. for use with CVSweb

Changes in RUnit 0.4.0

o   New 'error' category DEACTIVATED introduced: If the function
    DEACTIVATED is inserted into a test function the function
    stops at that point and is reported as deactivated in the test

o   New function 'getErrors' which takes a list of type
    'RUnitTestData' and returns some very basic error information
    of a test run.

o   The name of the currently executed test function is written to
    standard out.

o   'printHTMLProtocol' fixed such that it does not produce a
    warning anymore.

o   Dependency on package 'splines' removed.

o   Various small fixes of the documentation.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.4.32 by Roman Zenka, 10 months ago

Browse source code at https://github.com/cran/RUnit

Authors: Matthias Burger <[email protected]> , Klaus Juenemann <[email protected]> , Thomas Koenig <[email protected]>

Documentation:   PDF Manual  

GPL-2 license

Depends on utils, methods, graphics

Suggests XML

Imported by BLCOP, geno2proteo, haploR, kutils, ldamatch, rkafkajars, runittotestthat.

Depended on by FKF, SoilR, discrimARTs, futile.paradigm, permGPU, rkafka.

Suggested by BioMedR, ChainLadder, ClimDown, CodeDepends, DistributionUtils, FeatureHashing, GeneralizedHyperbolic, HistogramTools, JuniperKernel, MetaIntegrator, NMF, NMOF, NanoStringNorm, NormalLaplace, NutrienTrackeR, PCICt, PMwR, RProtoBuf, RQuantLib, Rblpapi, Rcpp, RcppAnnoy, RcppArmadillo, RcppCCTZ, RcppClassic, RcppClassicExamples, RcppDynProg, RcppEigen, RcppExamples, RcppGSL, RcppParallel, RcppRedis, RecordLinkage, RobAStBase, RobExtremes, SkewHyperbolic, TKF, TTR, VFP, VarianceGamma, WVPlots, WeightedCluster, XLConnect, Xmisc, alphastable, aucm, bbmle, cccp, cdata, chngpt, classyfire, cleanr, climdex.pcic, clusterCrit, cmaes, doParallel, doSNOW, document, emoa, fAsianOptions, fAssets, fBasics, fBonds, fCopulae, fExoticOptions, fExtremes, fGarch, fImport, fMultivar, fNonlinear, fOptions, fRegression, fTrading, fUnitRoots, fakemake, fastdigest, fingerprint, gMCP, gbm, gdata, glarma, hypergea, iterators, krm, kyotil, lisp, logitnorm, matrixpls, mcr, mdw, nCal, nanotime, ncdf4.helpers, nlcv, nplr, orgutils, pdmod, pkgmaker, pls, prc, protViz, qmrparser, rasciidoc, rcdk, restfulr, rngtools, robustrank, rqdatatable, rquery, rredis, rsolr, rstan, sigr, simcausal, sp23design, spatialprobit, spcosa, sptm, stabledist, timeDate, timeSeries, vtreat, wrapr, xts, yaml.

Enhanced by doMC, foreach.

See at CRAN