MyNixOS website logo
Description

Testing for R Packages with Multiple Attempts for Noisy Tests.

Runs tests using the 'testthat' package but allows for multiple attempts for a single test. This is useful for noisy or flaky tests that generally pass but can fail due to occasional random errors, such as numeric instability or using random data.

testthatmulti

R-CMD-check Codecov testcoverage

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 3 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
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • 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