local_test_context: Locally set options for maximal test reproducibility

Description Usage Arguments Details Examples

View source: R/local.R

Description

local_test_context() is run automatically by test_that() but you may want to run it yourself if you want to replicate test results interactively. If run inside a function, the effects are automatically reversed when the function exits; if running in the global environment, use withr::deferred_run() to undo.

local_reproducible_output() is run automatically by test_that() in the 3rd edition. You might want to call it to override the the default settings inside a test, if you want to test Unicode, coloured output, or a non-standard width.

Usage

1
2
3
4
5
6
7
8
local_test_context(.env = parent.frame())

local_reproducible_output(
  width = 80,
  crayon = FALSE,
  unicode = FALSE,
  .env = parent.frame()
)

Arguments

.env

Environment to use for scoping; expert use only.

width

Value of the "width" option.

crayon

Value of the "crayon.enabled" option.

unicode

Value of the "cli.unicode" option. The test is skipped if l10n_info()$`UTF-8` is FALSE.

Details

local_test_context() sets TESTTHAT = "true", which ensures that is_testing() returns TRUE and allows code to tell if it is run by testthat.

In the third edition, local_test_context() also calls local_reproducible_output() which temporary sets the following options:

And modifies the following env vars:

Finally, it sets the collation locale to "C", which ensures that character sorting the same regardless of system locale.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
local({
  local_test_context()
  cat(crayon::blue("Text will not be colored"))
  cat(cli::symbol$ellipsis)
  cat("\n")
})
test_that("test ellipsis", {
  local_reproducible_output(unicode = FALSE)
  expect_equal(cli::symbol$ellipsis, "...")

  local_reproducible_output(unicode = TRUE)
  expect_equal(cli::symbol$ellipsis, "\u2026")
})

r-lib/testthat documentation built on Sept. 25, 2021, 3:02 p.m.