Skip to content

Calling teardown_env() when testing Setup.R #2031

@ralmond

Description

@ralmond

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.

Created on 2024-11-22 with reprex v2.1.1

Here is the backtrace:

Backtrace:
    ▆
 1. ├─base::source("~/ralmond1/Projects/Peanut/tests/testthat/setup.R")
 2. │ ├─base::withVisible(eval(ei, envir))
 3. │ └─base::eval(ei, envir)
 4. │   └─base::eval(ei, envir)
 5. ├─withr::defer(...) at tests/testthat/setup.R:16:1
 6. └─testthat::teardown_env()

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions