Learn R Programming

testthatmulti

The goal of testthatmulti is to enhance the R package testthat so that noisy tests can be run. If a test fails 1% of the time, it can cause problems with R packages. By allowing each test multiple attempts to pass, unexpected and rare test failures can be avoided.

Installation

You can install the development version of testthatmulti from GitHub with:

# install.packages("devtools")
devtools::install_github("CollinErickson/testthatmulti")

Example

For example, suppose you have a test that passes 99% of the time. This can happen when tests are run using randomly generated data, such as for modeling or optimization.

set.seed(153)
library(testthat)
x <- runif(1)
cat('x is', x, '\n')
#> x is 0.9975921
expect_true(x < .99)
#> Error: x < 0.99 is not TRUE
#> 
#> `actual`:   FALSE
#> `expected`: TRUE

This test will usually pass, but could fail occasionally and cause problems when submitting to CRAN.

To give the test multiple chances to pass, wrap the function ttm around the code for the tests and tell it how many times to run the tests. It will succeed if all the tests are passed on any single iteration. In the code for the tests, replace expect_true with ttm_expect_true and expect_equal with ttm_expect_equal.

Below, we run the same test as above, but give it 3 chances to pass. By giving it 10 attempts to pass the test, the probability of failure drops from 1% to 0.0001%.

set.seed(153)
library(testthatmulti)
ttm(10, {
  x <- runif(1)
  cat('x is', x, '\n')
  ttm_expect_true(x < .99)
})
#> x is 0.9975921 
#> x is 0.4970875

Copy Link

Version

Install

install.packages('testthatmulti')

Monthly Downloads

172

Version

0.2.0

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Collin Erickson

Last Published

August 24th, 2025

Functions in testthatmulti (0.2.0)

ttm

Test that with multiple attempts
ttm_i

Iteration count for ttm()
ttm_n

Max number of attempts for ttm()
ttm_expect_error

Test that multi: expect error
ttm_expect_true

Test that multi: expect true
ttm_expect_no_error

Test that multi: expect no error
ttm_expect_equal

Test that multi: expect equal