CRAN version badge CRAN Checks CRAN RStudio mirror total downloads badge CRAN RStudio mirror monthly downloads badge R-CMD-check DOI

unittest

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 “Adding Tests to Packages” vignette.

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

To install the latest development version, use remotes:

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