| test_package {testthat} | R Documentation |
Run all tests in a package
Description
-
test_local()tests a local source package. -
test_package()tests an installed package. -
test_check()checks a package duringR CMD check.
Tests live in tests/testthat.
Usage
test_package(package, reporter = check_reporter(), ...)
test_check(package, reporter = check_reporter(), ...)
test_local(path = ".", reporter = NULL, ...)
Arguments
package |
If these tests belong to a package, the name of the package. |
reporter |
Reporter to use to summarise output. Can be supplied
as a string (e.g. "summary") or as an R6 object
(e.g. See Reporter for more details and a list of built-in reporters. |
... |
Additional arguments passed to |
path |
Path to directory containing tests. |
Value
A list (invisibly) containing data about the test results.
R CMD check
To run testthat automatically from R CMD check, make sure you have
a tests/testthat.R that contains:
library(testthat)
library(yourpackage)
test_check("yourpackage")
Special files
There are two types of .R file that have special behaviour:
Test files start with
testand are executed in alphabetical order.Setup files start with
setupand are executed before tests. If clean up is needed after all tests have been run, you can usewithr::defer(clean_up(), teardown_env()). Seevignette("test-fixtures")for more details.
There are two other types of special file that we no longer recommend using:
Helper files start with
helperand are executed before tests are run. They're also loaded bydevtools::load_all(), so there's no real point to them and you should just put your helper code inR/.Teardown files start with
teardownand are executed after the tests are run. Now we recommend interleave setup and cleanup code insetup-files, making it easier to check that you automatically clean up every mess that you make.
All other files are ignored by testthat.
Environments
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.