Skip all testthat tests when condition not met
Asked Answered
B

2

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?

Beatify answered 9/1, 2016 at 8:7 Comment(1)
In that example, you would need if( fail_test() ) ...Myosin
A
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.

Anxiety answered 8/7, 2017 at 21:43 Comment(2)
Should you put this in all tests? Is there some way to run it at the "top level" only once?Heerlen
Updated link: r-pkgs.org/testing-advanced.html#when-testing-gets-hardUis
J
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:

Jamin answered 6/2, 2016 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.