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.
test_that(desc, code)
Test name. Names should be brief, but evocative. They are only used by humans, so do you
Test code containing expectations. Braces ({}
) should always
be used in order to get accurate location data for test failures.
When run interactively, returns invisible(TRUE)
if all tests
pass, otherwise throws an error.
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)
})
#> Test passed 🥇
if (FALSE) {
test_that("trigonometric functions match identities", {
expect_equal(sin(pi / 4), 1)
})
}