test: Test a condition

View source: R/test.R

testR Documentation

Test a condition

Description

Evaluate or compare an expression based on a reference expression, or test for errors. The comparison of expression values is done using base::all.equal().

Usage

test(
  key = "test",
  x,
  y = TRUE,
  error = FALSE,
  warning = FALSE,
  runOnly = FALSE,
  text = "",
  onFail = c("fail", "warn", "ignore", "skip"),
  ...
)

Arguments

key

Identifier for the test condition.

x

The expression to test.

y

The reference value or expression.

error

Whether to expect an error, or the pattern that the error message should match.

warning

Whether to expect a warning, or the pattern that the warning message should match.

runOnly

Whether to skip the comparison to y. If disabled, an error is raised when y is not TRUE.

text

Human-readable description of what this test condition is testing.

onFail

How to handle a failure of the test condition. By default, this is recorded as a test failure. If onFail = "skip", the condition is not tested.

...

Additional arguments passed to base::all.equal()

Details

Inspired by the data.table package test mechanism. The original motivation for this function is the lack of R support for a proper stack trace with line numbers when sourcing files, which made it practically impossible to identify the offending line in a sourced file.

Value

Whether the test condition has passed successfully. In case of onFail = "skip", NA is returned.

Examples

## Not run: 
test('gt', 2 > 1)
test('eq', 1 + 1, 2)
test('lt', 2 < 1, onFail = "warn")

## End(Not run)

latrend documentation built on March 31, 2023, 5:45 p.m.