Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip all testthat tests when condition not met

What is the proper way to skip all tests in the test directory of an R package when using testthat/devtools infrastructure? For example, if there is no connection to a database and all the tests rely on that connection, do I need to write a skip in all the files individually or can I write a single skip somewhere?

I have a standard package setup that looks like

mypackage/

  • ... # other package stuff
  • tests/
    • testthat.R
    • testthat/
      • test-thing1.R
      • test-thing2.R

At first I thought I could put a test in the testthat.R file like

## in testthat.R
library(testthat)
library(mypackage)

fail_test <- function() FALSE
if (fail_test()) test_check("package")

but, that didn't work and it looks like calling devtools::test() just ignores that file. I guess an alternative would be to store all the tests in another directory, but is there a better solution?

like image 301
Rorschach Avatar asked Jan 09 '16 08:01

Rorschach


1 Answers

The Skipping a test section in the R Packages book covers this use case. Essentially, you write a custom function that checks whatever condition you need to check -- whether or not you can connect to your database -- and then call that function from all tests that require that condition to be satisfied.

Example, parroted from the book:

skip_if_no_db <- function() {
  if (db_conn()) {
    skip("API not available")
  }
}

test_that("foo api returns bar when given baz", {
  skip_if_no_db()
  ...
})

I've found this approach more useful than a single switch to toggle off all tests since I tend to have a mix of test that do and don't rely on whatever condition I'm checking and I want to always run as many tests as possible.

like image 152
Dylan Avatar answered Oct 14 '22 14:10

Dylan