Testing with
Edge Cases in R
Learn to design robust tests using boundary values, missing data, and unexpected inputs to write R code that never breaks in production.
What Is an Edge Case?
An edge case is an input or scenario at the extreme boundary of what your function is designed to handle. These are the values most likely to reveal hidden bugs: empty vectors, \( \text{NA} \) values, very large numbers, or inputs of the wrong type.
Why Edge Cases Matter
The Testing Landscape in R
The Core Rule of Edge Case Testing
Categories of Edge Cases
NA vs NaN vs NULL
| \( \text{NA} \) | \( \text{NaN} \) | \( \text{NULL} \) | |
|---|---|---|---|
| Meaning | Missing / unknown value | Not a number (\( \frac{0}{0} \)) | Absence of a value |
| \( \text{is.na()} \) returns | \( \text{TRUE} \) | \( \text{TRUE} \) | \( \text{logical(0)} \) |
| \( \text{is.null()} \) returns | \( \text{FALSE} \) | \( \text{FALSE} \) | \( \text{TRUE} \) |
| \( \text{length()} \) | \( 1 \) | \( 1 \) | \( 0 \) |
| In a vector | Keeps its slot | Keeps its slot | Silently dropped |
Step-by-Step Testing Workflow
Example 1: safe_mean()
Let's write a function that computes the mean of a numeric vector but handles edge cases gracefully.
Testing safe_mean()
Example 2: safe_divide()
A function that divides two numbers but handles division by zero and non-numeric inputs.
Example 3: Testing a Data Frame Function
Key expect_*() Functions
| Function | What It Tests | Edge Case Use |
|---|---|---|
| \( \text{expect\_equal(a, b)} \) | a equals b (with tolerance) | Normal output values |
| \( \text{expect\_true(x)} \) | x is TRUE | \( \text{is.na()} \), \( \text{is.null()} \) checks |
| \( \text{expect\_error(expr)} \) | Expression throws an error | Wrong type, missing column |
| \( \text{expect\_warning(expr)} \) | Expression produces a warning | Coercion warnings |
| \( \text{expect\_length(x, n)} \) | \( \text{length(x) == n} \) | Empty/single outputs |
| \( \text{expect\_type(x, type)} \) | \( \text{typeof(x)} \) matches | Return type consistency |
Common Mistakes
Edge Case Cheat Sheet
| Input Type | Edge Case | Test With |
|---|---|---|
| Numeric vector | \( \text{numeric(0)} \), \( \text{c(NA)} \), \( \text{Inf} \) | expect_true(is.na(...)) |
| Character vector | \( "" \), \( \text{character(0)} \) | expect_length(..., 0) |
| Data frame | 0-row, missing column | expect_error(...) |
| Single value | \( \text{NA} \), \( \text{NULL} \), \( \text{NaN} \) | expect_true(is.na(...)) |
| Integer | \( 0 \), negative, \( \text{.Machine\$integer.max} \) | expect_equal(...) |
| Any argument | Wrong type passed | expect_error(...) |
Question 1 of 5
What does \( \text{mean(c(1, NA, 3))} \) return in R without setting \( \text{na.rm = TRUE} \)?
Question 2 of 5
Which testthat function should you use to verify that a function throws an error when given invalid input?
Question 3 of 5
What is the length of \( \text{NULL} \) in R?
Question 4 of 5
You write a function that accepts a numeric vector. Which of the following is NOT a useful edge case to test?
Question 5 of 5
What does \( \text{is.na(NaN)} \) return in R?