Conditionally stop testing a tinytest test file if a required package is not available or not of a minimum required version

skip_if_not_installed(pkg, minimum_version = NULL, quietly = TRUE)

exit_if_not_installed(pkg, minimum_version = NULL, quietly = TRUE)

Arguments

pkg

Name of package to check for

minimum_version

Optional minimum required version of pkg

quietly

Attempt to find the package quietly; passed to requireNamespace()

Value

If called within a tinytest test and pkg is either not installed or not at least minimum_version, triggers an exit condition; otherwise, returns one of

  • A string saying that pkg is not installed

  • A string saying that pkg is installed, but not at least minimum_version

  • NULL invisibly

“Skip” vs “Exit”

tinyexpect provides both “skip_” and “exit_” versions of “stop testing” functions due to the different philosophies of tinytest and testthat; in testthat, tests are encapsulated by test_that() to create smaller testing units within a single test file. As such, if a series of tests need to be passed over for some reason, it makes sense to “skip” a test_that() block and move on to the next block

tinytest, however, treats each test file as a testing unit. Each file in inst/tinytest is equivalent to a testthat test_that() block; as such, if a series of tests need to be passed over for some reason, it makes sense to “exit” a test file and move on to the next file in inst/tinytest

In order to provide compatibility with users transitioning from testthat to tinytest, and to provide continuity with the tinytest philosophy, tinyexpect provides both skip_- and exit_- prefixed “stop testing” functions that work identically to one another

Examples

(pkg <- paste(sample(letters, size = 7L, replace = TRUE), collapse = ""))
#> [1] "fivjubq"
skip_if_not_installed(pkg)
#> [1] "fivjubq cannot be loaded"

skip_if_not_installed("tinyexpect", minimum_version = "99.0.1")
#> [1] "Installed version of tinyexpect is 0.0.0.9010, but 99.0.1 is required"