test_dir() for a collection of tests in a directory; use
test_package() interactively at the console, and
R CMD check.
In your own code, you can use
is_testing() to determine if code is being
run as part of a test and
testing_package() to retrieve the name of the
package being tested. You can also check the underlying env var directly
identical(Sys.getenv("TESTTHAT"), "true") to avoid creating a run-time
dependency on testthat.
test_dir(path, filter = NULL, reporter = default_reporter(), env = test_env(), ..., encoding = "unknown", load_helpers = TRUE, stop_on_failure = FALSE, stop_on_warning = FALSE, wrap = TRUE)
test_package(package, filter = NULL, reporter = check_reporter(), ..., stop_on_failure = TRUE, stop_on_warning = FALSE)
test_check(package, filter = NULL, reporter = check_reporter(), ..., stop_on_failure = TRUE, stop_on_warning = FALSE, wrap = TRUE)
Path to directory containing tests.
NULL, only tests with file names matching this
regular expression will be executed. Matching be performed on the file
name after it has been stripped of
Reporter to use to summarise output. Can be supplied
as a string (e.g. "summary") or as an R6 object
See Reporter for more details and a list of built-in reporters.
Environment in which to execute the tests. Expert use only.
Additional arguments passed to
grepl() to control filtering.
Deprecated. All files now assumed to be UTF-8.
Source helper files before running the tests?
source_test_helpers() for more details.
TRUE, throw an error if any tests fail.
TRUE, throw an error if any tests generate
Automatically wrap all code within
test_that()? This ensures
that all expectations are reported, even if outside a test block.
Name of installed package.
A list of test results.
For package code, tests should live in
There are four classes of
.R files that have special behaviour:
Test files start with
test and are executed in alphabetical order.
Helper files start with
helper and are executed before tests are
run and from
Setup files start with
setup and are executed before tests, but not
Teardown files start with
teardown and are executed after the tests
Each test is run in a clean environment to keep tests as isolated as possible. For package tests, that environment that inherits from the package's namespace environment, so that tests can access internal functions and objects.
To run testthat automatically from
R CMD check, make sure you have
tests/testthat.R that contains: