Learn R Programming

⚠️There's a newer version (3.2.3) 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("devtools")
devtools::install_github("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

578,511

Version

2.3.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hadley Wickham

Last Published

December 1st, 2019

Functions in testthat (2.3.1)

Reporter

Manage test reporting
FailReporter

Test reporter: fail at end.
TapReporter

Test reporter: TAP format.
SummaryReporter

Test reporter: summary of errors.
capture_condition

Capture conditions, including messeages, warnings, expectations, and errors.
DebugReporter

Test reporter: start recovery.
LocationReporter

Test reporter: location
StopReporter

Test reporter: stop on error.
MultiReporter

Multi reporter: combine several reporters in one.
JunitReporter

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

Test reporter: RStudio
auto_test

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

The building block of all expect_ functions
evaluate_promise

Evaluate a promise, capturing all types of output.
TeamcityReporter

Test reporter: Teamcity format.
auto_test_package

Watches a package for changes, rerunning tests as appropriate.
SilentReporter

Test reporter: gather all errors silently.
compare_state

Compare two directory states.
dir_state

Capture the state of a directory.
compare

Provide human-readable comparison of two objects
equality-expectations

Expectation: is the object equal to a value?
expect_null

Expectation: is an object NULL?
expect_named

Expectation: does object have names?
expect_that

Expect that a condition holds.
not

Negate an expectation
expect_vector

Expectation: does the object have vctr properties?
expect_invisible

Expectation: does expression return visibily or invisibly?
oldskool

Old-style expectations.
capture_output

Capture output to console
comparison-expectations

Expectation: is returned value less or greater than specified value?
fail

Default expectations that always succeed or fail.
expect_is

Expectation: does the object inherit from a given class?
expectation

Construct an expectation object
skip

Skip a test.
context

Describe the context of a set of tests.
default_reporter

Retrieve the default reporter
describe

describe: a BDD testing language
source_file

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

Run code on setup/teardown
takes_less_than

Expectation: does expression take less than a fixed amount of time to run?
is_informative_error

Is an error informative?
inheritance-expectations

Expectation: does the object inherit from a S3 or S4 class, or is it a base type?
find_reporter

Find reporter object given name or object.
find_test_scripts

Find the test files.
expect_length

Expectation: does a vector have the specified length?
expect_known_output

Expectations: is the output or the value equal to a known good value?
reporter-accessors

Get and set active reporter.
testthat_results

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

Try evaluating an expressing multiple times until it succeeds.
safe_digest

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

Expectation: does code throw error or other condition?
expect_setequal

Expectation: do two vectors contain the same values?
expect_output

Expectation: does code print output to the console?
expect_cpp_tests_pass

Expectation: do C++ tests past?
expect_match

Expectation: does string match a regular expression?
expect_message

Expectation: does code produce warnings or messages?
quasi_label

Quasi-labelling
test_file

Run all tests in specified file
test_examples

Test package examples
expect_silent

Expectation: is the code silent?
logical-expectations

Expectation: is the object true/false?
expect_success

Tools for testing expectations
watch

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

Objects exported from other packages
with_mock

Mock functions in a package.
test_env

Generate default testing environment.
testthat_examples

Retrieve paths to built-in example test files
test_dir

Run all tests in directory or package
testthat-package

R package to make testing fun!
test_that

Create a test.
verify_output

Verify output
make_expectation

Make an equality test.
use_catch

Use Catch for C++ Unit Testing
test_path

Locate file in testing directory.
MinimalReporter

Test reporter: minimal.
ProgressReporter

Test reporter: interactive progress bar of errors.
CheckReporter

Check reporter: 13 line summary of problems
ListReporter

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