Learn R Programming

testthat (version 3.2.2)

teardown: Run code before/after tests

Description

[Superseded]

We no longer recommend using setup() and teardown(); instead we think it's better practice to use a test fixture as described in vignette("test-fixtures").

Code in a setup() block is run immediately in a clean environment. Code in a teardown() block is run upon completion of a test file, even if it exits with an error. Multiple calls to teardown() will be executed in the order they were created.

Usage

teardown(code, env = parent.frame())

setup(code, env = parent.frame())

Arguments

code

Code to evaluate

env

Environment in which code will be evaluated. For expert use only.

Examples

Run this code
if (FALSE) {
# Old approach
tmp <- tempfile()
setup(writeLines("some test data", tmp))
teardown(unlink(tmp))
}

# Now recommended:
local_test_data <- function(env = parent.frame()) {
  tmp <- tempfile()
  writeLines("some test data", tmp)
  withr::defer(unlink(tmp), env)

  tmp
}
# Then call local_test_data() in your tests

Run the code above in your browser using DataLab