7

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?

Community
  • 1
  • 1
Rorschach
  • 31,301
  • 5
  • 78
  • 129

2 Answers2

7

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.

Dylan
  • 745
  • 8
  • 10
1

Maybe you may organize tests in subdirectories, putting conditional directory inclusion in a parent folder test:

Consider 'tests' in testthat package. In particular, this one looks interesting:

I do not see here nothing that recurses subdirectories in test scan:

hute37
  • 197
  • 2
  • 8