MyNixOS website logo
Description

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.

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:

  • source('test_myfunction.R') from R
  • Rscript --vanilla test_myfunction.R from the command prompt
  • 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 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:

  • ok(ut_cmp_equal( biggest(1/3, 2/6), 2/6), "two floating point numbers"): Uses all.equal to compare within a tolerance.
  • ok(ut_cmp_identical( biggest("c", "d") ), "two strings"): Uses identical to make sure outputs are identical.
  • 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, 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")
Metadata

Version

1.7-0

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows