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

test_that

Create a test.


Description

A test encapsulates a series of expectations about small, self-contained set of functionality. Each test is contained in a context and contains multiple expectations.

Usage

test_that(desc, code)

Arguments

desc

test name. Names should be kept as brief as possible, as they are often used as line prefixes.

code

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

Details

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

When run from the command line, tests return NULL if all expectations are met, otherwise it raises an error.

Examples

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)
})
# Failing test:
## Not run: 
test_that("trigonometric functions match identities", {
  expect_equal(sin(pi / 4), 1)
})

## End(Not run)

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.