How to add functions only for test in testthat for R?

31 Views Asked by At

In my package, I would like to add a few functions which are only used for test to check whether a test database can be connected. For example for file tests/testthat.R

library(testthat)
library(mypackage)
is_test_db <- function() {
    x <- try({
        con <- db_connect()
        on.exit(db_disconnect(con), add = TRUE)
    })
    if (inherits(x, "try-error")) {
        return(FALSE)
    }
    return(TRUE)
}
test_check("mypackage")

However, the function is_test_db cannot be found in all tests files. How could I define a new function only for all tests?

0

There are 0 best solutions below