Learn R Programming

testthat (version 3.2.1)

test_that: Run a test

Description

A test encapsulates a series of expectations about a small, self-contained unit of functionality. Each test contains one or more expectations, such as expect_equal() or expect_error(), and lives in a test/testhat/test* file, often together with other tests that relate to the same function or set of functions.

Each test has its own execution environment, so an object created in a test also dies with the test. Note that this cleanup does not happen automatically for other aspects of global state, such as session options or filesystem changes. Avoid changing global state, when possible, and reverse any changes that you do make.

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. It's common to write the description so that it reads like a natural sentence, e.g. test_that("multiplication works", { ... }).

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