Unit Testing with
testthat
Learn how to write reliable, automated tests for your R code using test_that() and expect_equal(): the foundation of professional R development.
What Is Unit Testing?
A unit test is a small, automated check that verifies one specific behavior of your code. Instead of manually running your function and eyeballing results, you write code that tests your code.
Why Use testthat?
testthat is the most popular testing framework for R. It gives you a clear, readable way to describe what your code should do and check that it actually does it.
The Two Building Blocks
Anatomy of test_that()
expect_*() callsAnatomy of expect_equal()
test_that() vs expect_equal()
test_that() |
expect_equal() |
|
|---|---|---|
| Role | Container / organizer | Individual assertion |
| Arguments | Description string + code block | Actual value + expected value |
| How many? | One per behavior | One or more per test_that() |
| Analogy | A test case envelope | A single True/False check inside |
| On failure | Reports which test failed | Reports what value was wrong |
The expect_* Family
expect_equal() is the most common, but testthat provides a whole family of assertion functions for different situations:
expect_equal()expect_true()expect_error()expect_length()Writing Tests: Step by Step
install.packages("testthat") once, then library(testthat)add())test_that() block: give it a clear description and add expect_equal() assertionsExample 1: Testing add()
Example 2: Testing Edge Cases
Example 3: When a Test Fails
The Testing Workflow
Floating-Point Tolerance
Computers store decimal numbers with tiny rounding errors. The value \(0.1 + 0.2\) is not exactly \(0.3\) internally. expect_equal() handles this with a built-in tolerance.
Common Mistakes
expect_equal(actual, expected).library(testthat)test_that() without loading testthat first. Always call library(testthat) at the top of your test file.test_that(). Each test should cover one specific behavior.Quick Reference Table
| Function | Purpose | Example |
|---|---|---|
test_that() |
Group related assertions | test_that("desc", { ... }) |
expect_equal() |
Check value equality | expect_equal(f(2), 4) |
expect_true() |
Check TRUE condition | expect_true(is.numeric(x)) |
expect_error() |
Check error is thrown | expect_error(f("bad")) |
expect_identical() |
Exact match (no tolerance) | expect_identical(x, "hello") |
expect_length() |
Check vector length | expect_length(v, 3) |
Question 1 of 5
What is the primary role of test_that() in the testthat framework?
test_that() is a container that groups one or more expect_*() assertions under a single human-readable description. It does not perform comparisons itself: that is the job of expect_equal() and other expect_*() functions.Question 2 of 5
Given the function square <- function(x) \(x^2\), which test is written correctly?
test_that(description, { expect_*(...) }). The description string must come first, followed by a code block containing expectations. Options A and D swap or omit arguments, and option B reverses test_that() and expect_equal().Question 3 of 5
In expect_equal(actual, expected), what does the first argument represent?
expect_equal() is always the actual result produced by your function. The second argument is the expected value you want it to match. Reversing them still runs but gives confusing error messages when the test fails.Question 4 of 5
Why does expect_equal(0.1 + 0.2, 0.3) pass, even though 0.1 + 0.2 == 0.3 returns FALSE in R?
expect_equal() accounts for floating-point imprecision by allowing a small tolerance (approximately \(1.5 \times 10^{-8}\) by default). The tiny rounding error in 0.1 + 0.2 falls within this tolerance, so the test passes. For exact matching, use expect_identical().Question 5 of 5
A student writes the following test for a function called greet() that should return "Hello, Alice!" when given the name "Alice":
The function greet is defined as:
The test fails. What is the most likely reason?