Search code examples
rtestthat

Why is helper file in testthat sourced twice


I run into trouble when using uuids in tests, as the helper file is sourced twice. Why does that happen? Is there a way to avoid second sourcing?

For a reproducible example, just create a new package, put a file called "helper-data.R" in /tests/testhat/ with th following content

if (!exists("test_ind")) {
  test_ind <- 1
  print(paste0("test_ind = ", test_ind))
  test_ind <- test_ind + 1
} else {
  print(paste0("test_ind = ", test_ind))
  test_ind <- test_ind + 1
}

and create an file "test-1.R" in /tests/testhat/ with th following empty test

context("test1")
test_that("test1", {
  # expect_equal(1, 1)
})

and you will see that test_ind is 2 in the end. I found this link but I don't see how that could solve my problem.

Update: Created issue on github-testthat


Solution

  • I just recieved a message that this is solved in the dev-version on Github.