Use different test setups for integration testing

What would be the best approach to run tests with two (or more) different setups.
Let´s say I have some integration tests querying a DB and there are two backends to make an DB connection that have some subtle differentes (could be any other scenario of that kind though).

Currently I use a setup-* file to initiate an db-connection and run the tests with backend 1. After some painful debugging caused by an error only occurring on backend 2 I now want all my integration tests also to be run for backend 2.

My goal is to have something like this:

testthat/
  |── setup-con_1.R
  |── setup-con_2.R
  |── test-db_query.R
test(setup = setup-con_1)
test(setup = setup-con_2)

For now I use an env var to make a distinction between different setups:

setup.R:

if (Sys.getenv("TEST_DB_CON") == "CON1") {
  # use backend 1
} else if (Sys.getenv("TEST_DB_CON") == "CON2") {
  # use backend 2
}

running tests:

# integration testing -----------------

## con 1----

Sys.setenv(TEST_DB_CON = "CON1")
testthis::test_integration(stop_on_failure = TRUE)
Sys.unsetenv("TEST_DB_CON")

## con 2 ----

Sys.setenv(TEST_DB_CON = "CON2")
testthis::test_integration(stop_on_failure = TRUE)
Sys.unsetenv("TEST_DB_CON")

That works but does not look very elegant to me. What are your thoughts/ experiences on this?

1 Like