Learn R Programming

⚠️There's a newer version (3.2.1.1) of this package.Take me there.

testthat

Overview

Testing your code can be painful and tedious, but it greatly increases the quality of your code. testthat tries to make testing as fun as possible, so that you get a visceral satisfaction from writing tests. Testing should be addictive, so you do it all the time. To make that happen, testthat:

  • Provides functions that make it easy to describe what you expect a function to do, including catching errors, warnings, and messages.

  • Easily integrates in your existing workflow, whether it’s informal testing on the command line, building test suites, or using R CMD check.

  • Displays test progress visually, showing a pass, fail, or error for every expectation. If you’re using the terminal or a recent version of RStudio, it’ll even colour the output.

testthat draws inspiration from the xUnit family of testing packages, as well as from many of the innovative ruby testing libraries, like rspec, testy, bacon and cucumber.

testthat is the most popular unit testing package for R and is used by thousands of CRAN packages.

If you’re not familiar with testthat, the testing chapter in R packages gives a good overview, along with workflow advice and concrete examples.

Installation

# Install the released version from CRAN
install.packages("testthat")

# Or the development version from GitHub:
# install.packages("pak")
pak::pak("r-lib/testthat")

Usage

The easiest way to get started is with usethis. Assuming you’re in a package directory, just run usethis::use_test("name") to create a test file, and set up all the other infrastructure you need. If you’re using RStudio, press Cmd/Ctrl + Shift + T (or run devtools::test() if not) to run all the tests in a package.

Copy Link

Version

Install

install.packages('testthat')

Monthly Downloads

442,445

Version

3.1.10

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

July 6th, 2023

Functions in testthat (3.1.10)

SilentReporter

Test reporter: gather all errors silently.
equality-expectations

Does code return the expected value?
expect_length

Does code return a vector with the specified length?
expect_less_than

Deprecated numeric comparison functions
describe

describe: a BDD testing language
expect_invisible

Does code return a visible or invisible object?
expect_error

Does code throw an error, warning, message, or other condition?
dir_state

Capture the state of a directory.
expect_is

Does an object inherit from a given class?
comparison-expectations

Does code return a number greater/less than the expected value?
context

Describe the context of a set of tests.
expect_known_output

Expectations: is the output or the value equal to a known good value?
expect_no_error

Does code run without error, warning, message, or other condition?
expect_setequal

Does code return a vector containing the expected values?
expect_reference

Does code return a reference to the expected object?
context_start_file

Start test context from a file name
default_reporter

Retrieve the default reporter
expect_null

Does code return NULL?
expect

The building block of all expect_ functions
expect_equivalent

Is an object equal to the expected value, ignoring attributes?
compare_state

Compare two directory states.
compare

Provide human-readable comparison of two objects
inheritance-expectations

Does code return an object inheriting from the expected base type, S3 class, or S4 class?
expect_snapshot_file

Snapshot testing for whole files
expect_snapshot_output

Snapshot helpers
expect_that

Expect that a condition holds.
fail

Default expectations that always succeed or fail.
expect_vector

Does code return a vector with the expected size and/or prototype?
expectation

Construct an expectation object
not

Negate an expectation
oldskool

Old-style expectations.
is_informative_error

Is an error informative?
expect_output_file

Expectations: is the output or the value equal to a known good value?
local_mocked_bindings

Mocking tools
local_test_context

Locally set options for maximal test reproducibility
expect_output

Does code print output to the console?
expect_snapshot

Snapshot testing
expect_named

Does code return a vector with (given) names?
local_test_directory

Locally set test directory options
find_test_scripts

Find test files
expect_silent

Does code execute silently?
expect_match

Does a string match a regular expression?
evaluate_promise

Evaluate a promise, capturing all types of output.
set_state_inspector

State inspected
skip

Skip a test
safe_digest

Compute a digest of a filename, returning NA if the file doesn't exist.
find_reporter

Find reporter object given name or object.
logical-expectations

Does code return TRUE or FALSE?
expect_success

Tools for testing expectations
expect_snapshot_value

Snapshot testing for values
is_testing

Determine testing status
reporter-accessors

Get and set active reporter.
local_edition

Temporarily change the active testthat edition
test_file

Run all tests in a single file
expect_cpp_tests_pass

Do C++ tests past?
test_examples

Test package examples
teardown

Run code before/after tests
local_snapshotter

Instantiate local snapshotting context
quasi_label

Quasi-labelling
snapshot_accept

Snapshot management
test_dir

Run all tests in a directory
skip_on_travis

Superseded skip functions
make_expectation

Make an equality test.
test_env

Generate default testing environment.
test_path

Locate a file in the testing directory
test_package

Run all tests in a package
set_max_fails

Set maximum number of test failures allowed before aborting the run
testthat_tolerance

Default numeric tolerance
watch

Watch a directory for changes (additions, deletions & modifications).
with_mock

Mock functions in a package.
try_again

Try evaluating an expressing multiple times until it succeeds.
teardown_env

Run code after all test files
test_that

Run a test
testthat-package

An R package to make testing fun!
use_catch

Use Catch for C++ Unit Testing
reexports

Objects exported from other packages
source_file

Source a file, directory of files, or various important subsets
verify_output

Verify output
takes_less_than

Does code take less than the expected amount of time to run?
testthat_results

Create a testthat_results object from the test results as stored in the ListReporter results field.
testthat_examples

Retrieve paths to built-in example test files
MinimalReporter

Test reporter: minimal.
CheckReporter

Check reporter: 13 line summary of problems
MultiReporter

Multi reporter: combine several reporters in one.
RStudioReporter

Test reporter: RStudio
JunitReporter

Test reporter: summary of errors in jUnit XML format.
ListReporter

List reporter: gather all test results along with elapsed time and file information.
FailReporter

Test reporter: fail at end.
ProgressReporter

Test reporter: interactive progress bar of errors.
LocationReporter

Test reporter: location
DebugReporter

Test reporter: start recovery.
Reporter

Manage test reporting
TeamcityReporter

Test reporter: Teamcity format.
TapReporter

Test reporter: TAP format.
StopReporter

Test reporter: stop on error
SummaryReporter

Test reporter: summary of errors.
auto_test

Watches code and tests for changes, rerunning tests as appropriate.
capture_condition

Capture conditions, including messages, warnings, expectations, and errors.
capture_output

Capture output to console
auto_test_package

Watches a package for changes, rerunning tests as appropriate.