insertExpectationsAddin: Creates testthat tests for selected code

View source: R/testthat_addins.R

insertExpectationsAddinR Documentation

Creates testthat tests for selected code

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}

Inserts relevant expect_* tests based on the evaluation of the selected code.

Example: If the selected code is the name of a data.frame object, it will create an expect_equal test for each column, along with a test of the column names.

Currently supports side effects (error, warnings, messages), data.frames, and vectors.

List columns in data.frames (like nested tibbles) are currently skipped.

See `Details` for how to set a key command.

Usage

insertExpectationsAddin(
  selection = NULL,
  insert = TRUE,
  indentation = 0,
  copy_env = FALSE
)

insertExpectationsCopyEnvAddin(
  selection = NULL,
  insert = TRUE,
  indentation = 0,
  copy_env = TRUE
)

Arguments

selection

String of code. (Character)

E.g. "stop('This gives an expect_error test')".

N.B. Mainly intended for testing the addin programmatically.

insert

Whether to insert the expectations via rstudioapi::insertText() or return them. (Logical)

N.B. Mainly intended for testing the addin programmatically.

indentation

Indentation of the selection. (Numeric)

N.B. Mainly intended for testing the addin programmatically.

copy_env

Whether to work in a deep copy of the environment. (Logical)

Side effects will be captured in copies of the copy, why two copies of the environment will exist at the same time.

Disabled by default to save memory but is often preferable to enable, e.g. when the function changes non-local variables.

Details

How

Parses and evaluates the selected code string within the parent environment (or a deep copy thereof). Depending on the output, it creates a set of unit tests (like expect_equal(data[["column"]], c(1,2,3))), and inserts them instead of the selection.

How to set up a key command in RStudio

After installing the package. Go to:

Tools >> Addins >> Browse Addins >> Keyboard Shortcuts.

Find "Insert Expectations" and press its field under Shortcut.

Press desired key command, e.g. Alt+E.

Press Apply.

Press Execute.

Value

Inserts testthat::expect_* unit tests for the selected code.

Returns NULL invisibly.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other expectation generators: gxs_function(), gxs_selection(), initializeGXSFunctionAddin()

Other addins: assertCollectionAddin(), dputSelectedAddin(), initializeGXSFunctionAddin(), initializeTestthatAddin(), navigateTestFileAddin(), wrapStringAddin()


xpectr documentation built on Nov. 18, 2022, 5:10 p.m.