Basics

test_that()

Run a test

expect_equal() expect_identical()

Do you expect this value?

expect_error() expect_warning() expect_message() expect_condition()

Do you expect an error, warning, message, or other condition?

expect_true() expect_false()

Do you expect TRUE or FALSE?

Expectations

Values

expect_all_equal() expect_all_true() expect_all_false()

Do you expect every value in a vector to have this value?

expect_lt() expect_lte() expect_gt() expect_gte()

Do you expect a value bigger or smaller than this?

expect_length() expect_shape()

Do you expect an object with this length or shape?

expect_match() expect_no_match()

Do you expect a string to match this pattern?

expect_named()

Do you expect a vector with (these) names?

expect_null()

Do you expect NULL?

expect_setequal() expect_mapequal() expect_contains() expect_in() expect_disjoint()

Do you expect a vector containing these values?

expect_type() expect_s3_class() expect_s4_class() expect_r6_class() expect_s7_class()

Do you expect an S3/S4/R6/S7 object that inherits from this class?

expect_vector()

Do you expect a vector with this size and/or prototype?

Side-effects

expect_no_error() expect_no_warning() expect_no_message() expect_no_condition()

Do you expect the absence of errors, warnings, messages, or other conditions?

expect_invisible() expect_visible()

Do you expect the result to be (in)visible?

expect_output()

Do you expect printed output to match this pattern?

expect_silent()

Do you expect code to execute silently?

Snapshot tests

expect_snapshot()

Do you expect this code to run the same way as last time?

expect_snapshot_value()

Do you expect this code to return the same value as last time?

expect_snapshot_file() announce_snapshot_file() compare_file_binary() compare_file_text()

Do you expect this code to create the same file as last time?

snapshot_accept() snapshot_reject() snapshot_review()

Accept or reject modified snapshots

snapshot_download_gh()

Download snapshots from GitHub

Test helpers

is_testing() is_parallel() is_checking() is_snapshot() testing_package()

Determine testing status

extract_test()

Extract a reprex from a failed expectation

local_edition() edition_get()

Temporarily change the active testthat edition

local_test_context() local_reproducible_output()

Temporarily set options for maximum reproducibility

set_state_inspector()

Check for global state changes

skip() skip_if_not() skip_if() skip_if_not_installed() skip_unless_r() skip_if_offline() skip_on_cran() local_on_cran() skip_on_os() skip_on_ci() skip_on_covr() skip_on_bioc() skip_if_translated()

Skip a test for various reasons

teardown_env()

Run code after all test files

try_again()

Evaluate an expectation multiple times until it succeeds

Run tests

test_dir()

Run all tests in a directory

test_file()

Run tests in a single file

test_package() test_check() test_local()

Run all tests in a package

test_path()

Locate a file in the testing directory

use_catch()

Use Catch for C++ unit testing

Mocking

local_mocked_bindings() with_mocked_bindings()

Temporarily redefine function definitions

local_mocked_s3_method() local_mocked_s4_method()

Mock S3 and S4 methods

local_mocked_r6_class()

Mock an R6 class

mock_output_sequence()

Mock a sequence of output from a function

Custom expectations

fail() pass()

Declare that an expectation either passes or fails

expect_success() expect_failure() expect_snapshot_failure() show_failure()

Test your custom expectations

Reporters

CheckReporter

Report results for R CMD check

DebugReporter

Interactively debug failing tests

FailReporter

Fail if any tests fail

JunitReporter

Report results in jUnit XML format

ListReporter

Capture test results and metadata

LocationReporter

Test reporter: location

MinimalReporter

Report minimal results as compactly as possible

MultiReporter

Run multiple reporters at the same time

ProgressReporter CompactProgressReporter ParallelProgressReporter

Report progress interactively

RStudioReporter

Report results to RStudio

SilentReporter

Silently collect and all expectations

SlowReporter

Find slow tests

StopReporter

Error if any test fails

SummaryReporter

Report a summary of failures

TapReporter

Report results in TAP format

TeamcityReporter

Report results in Teamcity format