unittest-package {unittest}R Documentation

TAP-compliant Unit Testing

Description

Concise TAP-compliant unit testing package. Authored unit tests can be run using R CMD check with minimal implementation overhead.

Details

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:

  1. source('test_myfunction.R') from R

  2. Rscript --vanilla test_myfunction.R from the command prompt

  3. R CMD check, if test_myfunction.R is inside the tests directory of mypackage being tested. ‘unittest’ doesn't require any further setup in your package

If writing tests as part of a package, see vignette("testing_packages", package='unittest').

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:

  1. ok(ut_cmp_equal( biggest(1/3, 2/6), 2/6), "two floating point numbers"): Uses all.equal to compare within a tolerance

  2. ok(ut_cmp_identical( biggest("c", "d") ), "two strings"): Uses identical to make sure outputs are identical

  3. ok(ut_cmp_error(biggest(3), '"y".*missing'), "single argument is an error"): Make sure the code produces an error matching the regular expression

In all cases you get detailed, colourised output on what the difference is if the test fails.

Author(s)

Maintainer: Jamie Lentin <lentinj@shuttlethread.com>, Anthony Hennessey <anthony.hennessey@protonmail.com>.

References

Inspired by Perl's Test::Simple (https://metacpan.org/pod/Test::Simple).

See Also

testthat, RUnit, svUnit.


[Package unittest version 1.6-1 Index]