ravingmantis / unittest

Unit testing for R
4 stars 1 forks source link

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

unittest: Concise, TAP-compliant, R package for writing unit tests

Given a simple function you'd like to test in the file myfunction.R:

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

A test script for this function test_myfunction.R would be:

library(unittest)

source('myfunction.R')  # Or library(mypackage) if part of a package

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

You can then run this test in several ways:

If writing tests as part of a package, see the "Adding Tests to Packages" vignette for more information.

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. There are several ut_cmp_* helpers designed to work with ok:

In all cases you get detailed, colourised output on what the difference is, for example:

Output of `ok(ut_cmp_identical(list(1,3,3,4), list(1,2,3,4)))`

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.

Installing from CRAN

In an R session type

install.packages('unittest')

Or add Suggests: unittest to your package's DESCRIPTION file.

Installing the latest development version directly from GitHub

To install the latest development version, use remotes:

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