Why is helper file in testthat sourced twice

做~自己de王妃 提交于 2019-12-13 14:01:55

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/47350561/why-is-helper-file-in-testthat-sourced-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!