You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to test my Setup.R file by sourcing the file. However, in that context, teardown_env() is not properly initialized so I get an error.
Here is a simple example:
handle<- file(tempfile())
withr::defer(close(handle),testthat::teardown_env())
#> Error in `testthat::teardown_env()`:#> ! `teardown_env()` has not been initialized#> ℹ This is an internal error that was detected in the testthat package.#> Please report it at <https://github.com/r-lib/testthat/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
I don't think this is a bug so much as a bit of missing functionality. It would be good to have a way to artificially call Setup.R and then emulate the teardown, so that I can validate that my Setup code is working.
The text was updated successfully, but these errors were encountered:
Okay, I found the workaround. I need to replace the call to teardown_env() in my Setup.R code with a reference to .GlobalEnv. Then I can call withr::deferred_run() to test my cleanup code.
It would be cool if there was an option which would make this available without tweaking my code.
Say:
teardown_env<-function ()
{
if (is.null(the$teardown_env)) {
if (isTRUE(getOption("testthat_test_setup"))) {
message("Run 'withr::deferred_run()' to simulate teardown.")
return(.GlobalEnv)
}
abort("`teardown_env()` has not been initialized", .internal=TRUE)
}
the$teardown_env
}
Then, setting options(testthat_test_setup=TRUE) would allow sourcing the Setup.R file during testing.
I'm trying to test my
Setup.R
file by sourcing the file. However, in that context,teardown_env()
is not properly initialized so I get an error.Here is a simple example:
Created on 2024-11-22 with reprex v2.1.1
Here is the backtrace:
I don't think this is a bug so much as a bit of missing functionality. It would be good to have a way to artificially call
Setup.R
and then emulate the teardown, so that I can validate that my Setup code is working.The text was updated successfully, but these errors were encountered: