Learn R Programming

⚠️There's a newer version (1.4.1) of this package.Take me there.

A brief overview of tinytest

Package setup

A quick way to set things up is as follows.

tinytest::setup_tinytest("pkgdir")

where pkgdir is a package source directory with a valid DESCRIPTION file

The setup is as follows.

  1. Files with names starting with test are in pkg/inst/tinytest, e.g. test_haha.R. Test files are R scripts interspersed with test commands, such as expect_equal(0, myfunc(1)).
  2. tinytest is added Suggests: in the DESCRIPTION file.
  3. A file named tinytest.R is set up in pkg/tests to make sure that tests

will be run by R CMD check.

Interactive package testing

Functiondescription
test_all("pkgdir")run all test files (pkg must be loaded).
build_install_test("pkgdir")build, install, and test in temp dir.
run_test_dir("pkgdir")run all test files in a directory (pkg must be loaded).
run_test_file("testfile")run a single test file (pkg must be loaded).

All functions return an object of class tinytests. Results can be printed to screen, summarized with summary or converted to data frame with as.data.frame for analyses. The option verbose (default: TRUE) toggles between showing test progress in the terminal.

Test functions

The syntax of test functions resembles that of testthat. For expectations comparing two results, the first argument represents the observed value while the second argument represents the desired value.

Functiondescription
expect_trueArgument must evaluate to TRUE
expect_falseArgument must evaluate to FALSE
expect_equalData and attributes of arguments must be equal
expect_equivalentData of arguments must be equal
expect_identicalTarget and current mustbe identical
expect_warningExpression must yield a warning
expect_errorExpression must yield an error

For tests in a script there is an alternative syntax in the style of RUnit. For each function of the form expect_lol there is a function of the form checkLol.

Print options

Test results (objects of class tinytests) have two printing modes: a long format and a short, one-line format. Information that is always shown includes:

  • File name and line number of failed test.
  • The test call that resulted in test failure.
  • The type of failure. This can be 'data' (for differences in variable content), 'attr' (for differences in attributes like column names), or 'xcpt' for exceptions (warnings, errors).

In long format, the test call and difference between desired and realized input are shown in full. Global printing options can be set with options(option=value).

Optiondefaultdescription
tt.pr.passesFALSEprint passing tests?
tt.pr.limit10how many results to print?
tt.pr.nlong3how many tests in long format?
tt.pr.colorTRUEprint colored output?

It is also possible to influence these options using print.tinytest.

Run tests for an installed package

For a package called haha that is tested with tinytest, any user that has haha and tinytest installed can run tests as follows.

library(haha)
library(tinytest)
run_test_dir( system.file("tinytest",package="haha") )

Skipping or ignoring tests

Use ignore(testfunction) to run a test but not include the result in the output.

# both tests run, but only second is recorded.
if ( ignore(expect_equal)(2, 1+1) ){
  expect_true( 1 > 0 )
}

Note the placement of brackets.

Use at_home() to detect whether a test is running interactively, or via test_package() (i.e. the way R CMD check will run it).

if ( at_home() ){
  # run tests requiring lots of time.
}

Comparing with data stored on file

Data can be loaded from pkg/inst/tinytest (or subdirectories). A simple test file might look like this.

desired <- read.csv("mycsvoutput.csv", stringsAsFactors=FALSE)
obtained <- compute_my_result()
expect_equal(obtained, desired)

If you wish to publish the package on CRAN, make sure that the files are small enough for the package to be acceptable. See the cran repository policy for explicit bounds on package size. Alternatively you can avoid installing the data and associated test files by adding them to .Rbuildignore.

More information

See the vignette.

vignette("using_tinytest", package="tinytest")

Copy Link

Version

Install

install.packages('tinytest')

Monthly Downloads

24,141

Version

0.9.4

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Mark der Loo

Last Published

June 5th, 2019

Functions in tinytest (0.9.4)

summary.tinytests

Tinytests object
build_install_test

build, install and test
setup_tinytest

Add tinytest to package source directory
tinytest

Tinytest constructor
run_test_file

Run an R file containing tests; gather results
expect_equal

Express expectations
test_package

Test a package during R CMD check
at_home

Detect not on CRANity
format.tinytest

Print a tinytest object
ignore

Ignore the output of an expectation
run_test_dir

Run all tests in a directory