test_dir: Run all tests in directory or package

Description Usage Arguments Value Test files Environments R CMD check Examples

View source: R/test-directory.R

Description

Use test_dir() for a collection of tests in a directory; use test_package() interactively at the console, and test_check() inside of R CMD check.

In your own code, you can use is_testing() to determine if code is being run as part of a test. You can also check the underlying env var directly identical(Sys.getenv("TESTTHAT"), "true") to avoid creating a run-time dependency on testthat.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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_repoter(), ...,
  stop_on_failure = TRUE, stop_on_warning = FALSE)

test_check(package, filter = NULL, reporter = check_repoter(), ...,
  stop_on_failure = TRUE, stop_on_warning = FALSE, wrap = TRUE)

is_testing()

Arguments

path

path to tests

filter

If not NULL, only tests with file names matching this regular expression will be executed. Matching will take on the file name after it has been stripped of "test-" and ".R".

reporter

reporter to use

env

environment in which to execute the tests

...

Additional arguments passed to grepl() to control filtering.

encoding

File encoding, default is "unknown" unknown.

load_helpers

Source helper files before running the tests?

stop_on_failure

If TRUE, throw an error if any tests fail.

stop_on_warning

If TRUE, throw an error if any tests generate warnings.

wrap

Automatically wrap all code within test_that()? This ensures that all expectations are reported, even if outside a test block.

package

package name

Value

The results of the reporter function on all test results.

The results as a "testthat_results" (list)

Test files

For package code, tests should live in tests/testthat.

There are four classes of .R files that have special behaviour:

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.

R CMD check

To run testthat automatically from R CMD check, make sure you have a tests/testthat.R that contains:

1
2
3
4
library(testthat)
library(yourpackage)

test_check("yourpackage")

Examples

1
## Not run: test_package("testthat")

testthat documentation built on Dec. 13, 2017, 5:03 p.m.