Learn R Programming

testthat (version 3.1.4)

test_that: Run a test

Description

A test encapsulates a series of expectations about a small, self-contained set of functionality. Each test lives in a file and contains multiple expectations, like expect_equal() or expect_error().

Tests are evaluated in their own environments, and should not affect global state.

Usage

test_that(desc, code)

Value

When run interactively, returns invisible(TRUE) if all tests pass, otherwise throws an error.

Arguments

desc

Test name. Names should be brief, but evocative. They are only used by humans, so do you

code

Test code containing expectations. Braces ({}) should always be used in order to get accurate location data for test failures.

Examples

Run this code
test_that("trigonometric functions match identities", {
  expect_equal(sin(pi / 4), 1 / sqrt(2))
  expect_equal(cos(pi / 4), 1 / sqrt(2))
  expect_equal(tan(pi / 4), 1)
})

if (FALSE) {
test_that("trigonometric functions match identities", {
  expect_equal(sin(pi / 4), 1)
})
}

Run the code above in your browser using DataLab