Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

expect

The building block of all expect_ functions


Description

Call expect() when writing your own expectations. See vignette("custom-expectation") for details.

Usage

expect(
  ok,
  failure_message,
  info = NULL,
  srcref = NULL,
  trace = NULL,
  trace_env = caller_env()
)

Arguments

ok

TRUE or FALSE indicating if the expectation was successful.

failure_message

Message to show if the expectation failed.

info

Character vector continuing additional information. Included for backward compatibility only and new expectations should not use it.

srcref

Location of the failure. Should only needed to be explicitly supplied when you need to forward a srcref captured elsewhere.

trace

An optional backtrace created by rlang::trace_back(). When supplied, the expectation is displayed with the backtrace.

trace_env

If is.null(trace), this is used to automatically generate a traceback running from test_code()/test_file() to trace_env. You'll generally only need to set this if you're wrapping an expectation inside another function.

Details

While expect() creates and signals an expectation in one go, exp_signal() separately signals an expectation that you have manually created with new_expectation(). Expectations are signalled with the following protocol:

  • If the expectation is a failure or an error, it is signalled with base::stop(). Otherwise, it is signalled with base::signalCondition().

  • The continue_test restart is registered. When invoked, failing expectations are ignored and normal control flow is resumed to run the other tests.

Value

An expectation object. Signals the expectation condition with a continue_test restart.

See Also


testthat

Unit Testing for R

v3.0.2
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], RStudio [cph, fnd], R Core team [ctb] (Implementation of utils::recover())
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.