Test your code
Steps
Check: Does 95 get an A? Does 42 get an F?
Steps
Check: Does 98 get an A+? Does 91 get an A−?
Steps
Check: Does 86.5 get a B+?
Steps
Check: Does 0.91 get an A−?
Task: With a partner, run the final steps on these scores.
| Score | 100 |
58 |
42 |
0.85 |
|---|---|---|---|---|
| Should get | A+ | F | F | B |
| Gets | ? | ? | ? | ? |
01:00
Regression
A bug where a change breaks something that used to work.
Unit test
Code that runs a function on a known input and checks its output against the output you expect.
@examples show users how to call a function; tests check that it still does what it promised.use_testthat() and use_test()tests/testthat/test-strsplit1.R
test_that() takes a description and a block of code.| Expectation | Passes when |
|---|---|
expect_equal(x, y) |
x equals y |
expect_type(x, type) |
x has type type |
expect_length(x, n) |
x has length n |
expect_true(x) |
x is TRUE |
expect_error(code, msg) |
code errors with msg |
expect_warning(code, msg) |
code warns with msg |
Task: Write a test that strsplit1("a,b,c", split = ",") gives a character vector (not a list) with three elements.
02:00
test()✔ | 3 | strsplit1
══ Results ═════════════════════════════════════════════════════════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 3 ]
load_all(), then runs every file in tests/testthat/.check() runs the tests too.Add a test that splits on a period, then run test():
tests/testthat/test-strsplit1.R
Failure ('test-strsplit1.R:12:3'): strsplit1() splits on a period
Expected `strsplit1("3.14", split = ".")` to equal `c("3", "14")`.
`actual`: "" "" "" ""
`expected`: "3" "14"
[ FAIL 1 | WARN 0 | SKIP 0 | PASS 3 ]
strsplit1(): "a,b,c".Edge case
An input at the boundary of what a function handles: e.g. empty, a single value, very large, missing, or containing unusual characters.
For strsplit1:
Question: Predict what each call returns. Which results are bugs?
02:00
Some inputs should fail. Test that they fail, and for the right reason.
strsplit1(c("a,b", "c,d"), ",")returns"a" "b". Where did"c,d"go?
Red: before fixing anything, write the test.
Green: stop() throws an error with your message.
R/strsplit1.R
fixed = TRUE fixes the period test.A test suite is like a fortified dam: each test is a stone set where a leak once broke through. Change the code all you like; the old bugs stay behind the wall.
| Command | Does | Shortcut |
|---|---|---|
load_all() |
loads code and data | Shift + L |
document() |
rebuilds help pages | Shift + D |
test() |
runs the tests | Shift + T |
check() |
runs CRAN’s checks | Shift + E |
Shortcuts are Ctrl/Cmd + Shift + a letter in Positron/RStudio.
zev_raw <- read_excel("data/raw/New_ZEV_Sales_Last_updated_07-17-2026_ada.xlsx",
sheet = "County")
zev <- zev_raw |>
rename(year = `Data Year`, quarter = Quarter, county = COUNTY,
fuel_type = FUEL_TYPE, make = MAKE, model = MODEL,
n_vehicles = `Number of Vehicles`) |>
mutate(in_state = county != "Out Of State")
# Nothing dropped; expectations still hold
stopifnot(
nrow(zev) == nrow(zev_raw),
all(zev$quarter %in% 1:4),
all(zev$n_vehicles >= 1)
)When the data are updated next quarter, a broken expectation fails loudly.
