In my understanding, anything put inside test_that()
should be compartmentalized, meaning that if I load a package in test_that()
, its functions and methods shouldn't be available in other test_that()
calls.
In the example below, there are 3 (empty) tests:
- In the first one, we can see that the method
as.data.frame.lm
is not available in the namespace. - In the second one, I load the package
parameters
, which provides the methodas.data.frame.lm
. In my understanding, this method should only be available in thistest_that()
call. - In the third one, we can see that the method is available.
Edit: the package parameters
no longer exports this method so I modified the example below but the rationale stays the same.
library(testthat)
test_that("foo 1", {
print("as.matrix.get_predicted" %in% methods(as.matrix))
})
#> [1] FALSE
#> ── Skip (???): foo 1 ───────────────────────────────────────────────────────────
#> Reason: empty test
test_that("foo 2", {
invisible(insight::get_parameters)
})
#> ── Skip (???): foo 2 ───────────────────────────────────────────────────────────
#> Reason: empty test
test_that("foo 3", {
print("as.matrix.get_predicted" %in% methods(as.matrix))
})
#> [1] TRUE
#> ── Skip (???): foo 3 ───────────────────────────────────────────────────────────
#> Reason: empty test
Why is that? Are there some workarounds?
Edit: I'm looking for a solution specific to testthat
, not another testing framework.