test_that | R Documentation |
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.
test_that(desc, code)
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.
|
code |
Test code containing expectations. Braces ( |
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)
})
## Not run:
test_that("trigonometric functions match identities", {
expect_equal(sin(pi / 4), 1)
})
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.