TAP-Compliant Unit Testing

Concise TAP < http://testanything.org/> compliant unit testing package. Authored tests can be run using CMD check with minimal implementation overhead.


This is a concise, TAP-compliant, R package for writing unit tests. Authored tests can be run with R CMD check with minimal implementation overhead.

The workhorse of the unittest package is the ok function which prints "ok" when the expression provided evaluates to TRUE and "not ok" if the expression evaluates to anything else or results in an error.

If you are writing a package see the "I'm writing a package, how do I put tests in it?" section in the package documentation.

The package was inspired by Perl's Test::Simple.

If you want more features there are other unit testing packages out there; see testthat, RUnit, svUnit.

A very simple example of usage

You have a simple function in the file myfunction.R that looks something like this

biggest <- function(x,y) { max(c(x,y)) }

To test this create a file called test_myfunction.R in the same directory containing

library(unittest, quietly = TRUE)

source('myfunction.R')

ok(biggest(3,4) == 4, "two numbers")    
ok(biggest(c(5,3),c(3,4)) == 5, "two vectors")    

Now in an R session source() the test file

source('test_myfunction.R')

and you will see the test results. That's it. Now each time you edit myfunction.R re-sourcing test_myfunction.R reloads your function and runs your unit tests.

Installing from CRAN

In an R session type

install.packages('unittest')

Installing the latest development version directly from GitHub

Linux

In an R session type:

pkg_file <- tempfile()
download.file(url = 'https://github.com/ravingmantis/unittest/archive/master.tar.gz', mode = 'wb', method = 'wget', destfile = pkg_file)
install.packages(pkg_file, repos = NULL, type = 'source')

Mac OSX / Windows

download.file may not support fetching https URLs. Alternatively, you can use the the CRAN package downloader to fetch the archive instead: pkg_file <- tempfile() downloader::download(url = 'https://github.com/ravingmantis/unittest/archive/master.tar.gz', mode = 'wb', destfile = pkg_file) install.packages(pkg_file, repos = NULL, type = 'source')

Alternatively use the devtools CRAN package

If you have the CRAN package devtools you can use this to install directly from github:

# install.packages("devtools")
devtools::install_github("ravingmantis/unittest")

News

CHANGES IN VERSION 1.3-0:

NEW VIGNETTES: * Getting Started * FAQ

NEW FEATURES: * Show stack traces when functions being tested fail

CHANGES IN VERSION 1.1-0:

NEW FEATURES: * New ok_group function for grouping unit tests. * Additional cookbook examples in the package documentation.

Reference manual

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

install.packages("unittest")

1.4-0 by Jamie Lentin, 14 days ago


Report a bug at https://github.com/ravingmantis/unittest/issues


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


Authors: Jamie Lentin [aut, cre] , Anthony Hennessey [aut]


Documentation:   PDF Manual  


GPL (>= 3) license


Suggests knitr, rmarkdown


Suggested by CorporaCoCo.


See at CRAN