r-testthat
Best practices for writing R package tests with testthat version 3+.
Skill by Garrick Aden-Buie (@gadenbuie)
Install
Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.
npx agentmarket install skill/posit-dev-r-testthat
Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.
Get the weekly diff
The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.
About
Use cases: design, testing, writing
Reviews
Write a reviewNo reviews yet. Installed it? Reviews come from verified installers only. How reviews work
Permissions
What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.
- Tools
- No tools found
- Secrets
- None asked for
- Install scripts
- None found
Network hosts it talks to 1
codeload.github.com· found in install command
Files it touches
~/.claude/skills/r-testthat
What's inside
- Skill name
r-testthat- Description
- Best practices for writing R package tests with testthat version 3+. Use this skill when writing or organizing tests for an R package or when improving existing tests that use testthat. It covers test structure and expectations, self-sufficient test design, proper cleanup with withr, fixtures, mocking external dependencies, snapshot testing, and BDD-style describe/it patterns.
- License
- MIT
- Metadata
- author: Garrick Aden-Buie (@gadenbuie) · version: 1.1
- Instructions
- 419 lines in SKILL.md · view on GitHub
Files (6)
- SKILL.md
- advanced.md · references/
- bdd.md · references/
- fixtures.md · references/
- mocking.md · references/
- snapshots.md · references/
SKILL.md (first lines)
# Testing R Packages with testthat
Modern best practices for R package testing using testthat 3+.
## Initial Setup
Initialize testing with testthat 3rd edition:
```r
usethis::use_testthat(3)
```
This creates `tests/testthat/` directory, adds testthat to `DESCRIPTION` Suggests with `Config/testthat/edition: 3`, and creates `tests/testthat.R`.
## File Organization
**Mirror package structure:**
- Code in `R/foofy.R` → tests in `tests/testthat/test-foofy.R`
- Use `usethis::use_r("foofy")` and `usethis::use_test("foofy")` to create paired files
**Special files:**
- `helper-*.R` - Helper functions and custom expectations, sourced before tests
- `setup-*.R` - Run during `R CMD check` only, not during `load_all()`
- `fixtures/` - Static test data files accessed via `test_path()`
## Test Structure
Tests follow a three-level hierarchy: **File → Test → Expectation**
### Standard Syntax
```r
test_that("descriptive behavior", {
result <- my_function(input)
expect_equal(result, expected_value)
})
```
**Test descriptions** should read naturally and describe behavior, not implementation.
### BDD Syntax (describe/it)
For behavior-driven development, use `describe()` and `it()`:
```r
describe("matrix()", {
it("can be multiplied by a scalar", {
m1 <- matrix(1:4, 2, 2)
m2 <- m1 * 2
expect_equal(matrix(1:4 * 2, 2, 2), m2)
})
it("can be transposed", {
m <- matrix(1:4, 2, 2)
expect_equal(t(m), matrix(c(1, 3, 2, 4), 2, 2))
})
})
```
**Key features:**
- `describe()` groups related specifications for a component
- `it()` defines individual specifications (like `test_that()`)
- Supports nesting for hierarchical organization
- `it()` without code creates pending test placeholders
**Use `describe()` to verify you implement the right things, use `test_that()` to ensure you do things right.**
See [references/bdd.md](references/bdd.md) for comprehensive BDD patterns, nested specifications, and test-first workflows.
## Running Tests
Three scales of testing:
**Micro** (interactive development):
```r
devtools::load_all()
expect_equal(foofy(...), expected)
```
**Mezzo** (single file):
```r
testthat::test_file("tests/testthat/test-foofy.R")
# RStudio: Ctrl/Cmd + Shift + T
```
**Macro** (full suite):
```r
devtools::test() # Ctrl/Cmd + Shift + T
devtools::check() # Ctrl/Cmd + Shift + E
```
## Core Expectations
### Equality
```r
expect_equal(10, 10 + 1e-7) # Allows numeric tolerance
expect_identical(10L, 10L) # Exact match required
expect_all_equal(x, expected) # Every element matches (v3.3.0+)
```
### Errors, Warnings, Messages
```r
expect_error(1 / "a")
expect_error(bad_call(), class = "specific_error_class")
expect_no_error(valid_call())
expect_warning(deprecated_func())
expect_no_warning(safe_func())
expect_message(informative_func())
expect_no_message(quiet_func())
```
### Pattern Matching
```r
expect_match("Testing is fun!", "Testing")From posit-dev/skills, MIT.
More from posit-dev/skills
- 0.815score
r-lifecycle
Garrick Aden-Buie (@gadenbuie)
Guidance for managing R package lifecycle according to tidyverse principles using the lifecycle package.
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
shiny-py-optimize
Garrick Aden-Buie (@gadenbuie)
Diagnose and fix performance problems in Shiny for Python apps — slow startup, sluggish interactions, blocking operations, and apps that struggle beyond a…
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
pr-create
Garrick Aden-Buie (@gadenbuie)
Creates a pull request from current changes, monitors GitHub CI, and debugs any failures until CI passes.
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
r-cli
Garrick Aden-Buie (@gadenbuie)
Comprehensive R package for command-line interface styling, semantic messaging, and user communication.
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
shiny-r-optimize
Garrick Aden-Buie (@gadenbuie)
Diagnose and fix performance problems in Shiny for R apps — slow startup, sluggish interactions, long-running blocking operations, and apps that struggle…
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
r-cran-extrachecks
Garrick Aden-Buie (@gadenbuie)
Prepare R packages for CRAN submission by checking for common ad-hoc requirements not caught by devtools::check().
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
critical-code-reviewer
Garrick Aden-Buie (@gadenbuie)
Rigorously review code or pull requests for correctness, security, accessibility, maintainability, tests, and edge cases.
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
review-testing
Garrick Aden-Buie (@gadenbuie)
Review test code for quality, design, and completeness after implementing a feature or fixing a bug.
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
r-mirai
Charlie Gao (@shikokuchuo)
Help users write correct R code for async, parallel, and distributed computing using mirai.
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
r-cran-status
Garrick Aden-Buie (@gadenbuie)
Look up an R package's live status on cran.r-project.org - submission/review state (queue, human review, waiting, archived, past version's fate) or R CMD…
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
describe-design
Garrick Aden-Buie (@gadenbuie)
Research a codebase and create architectural documentation describing how features or systems work.
SkillAFreshness: A★ 547Claude Code +9 - 0.806score
pr-threads-resolve
Barret Schloerke (@schloerke)
Bulk resolve unresolved PR review threads on the current branch’s PR — typically after threads have been addressed manually or via /pr-threads-address
SkillAFreshness: A★ 547Claude Code +9
Signals
- Stars
- 547
- Forks
- 57
- Open issues
- 6
- Last commit (this folder)
- Oct 6, 2026 (21 days ago)
From GitHub (fetched Oct 24, 2026).
Details
- Version
- 1.1
- License
- MIT
- Repository
- github.com/posit-dev/skills
- First seen
- Oct 24, 2026
- Last checked
- Oct 24, 2026
- Source
- GitHubgithub-graphql
More from Garrick Aden-Buie (@gadenbuie)
- 0.815score
r-lifecycle
Garrick Aden-Buie (@gadenbuie)
Guidance for managing R package lifecycle according to tidyverse principles using the lifecycle package.
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
pr-create
Garrick Aden-Buie (@gadenbuie)
Creates a pull request from current changes, monitors GitHub CI, and debugs any failures until CI passes.
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
shiny-r-optimize
Garrick Aden-Buie (@gadenbuie)
Diagnose and fix performance problems in Shiny for R apps — slow startup, sluggish interactions, long-running blocking operations, and apps that struggle…
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
shiny-py-optimize
Garrick Aden-Buie (@gadenbuie)
Diagnose and fix performance problems in Shiny for Python apps — slow startup, sluggish interactions, blocking operations, and apps that struggle beyond a…
SkillAFreshness: A★ 547Claude Code +9 - 0.815score
r-cli
Garrick Aden-Buie (@gadenbuie)
Comprehensive R package for command-line interface styling, semantic messaging, and user communication.
SkillAFreshness: A★ 547Claude Code +9 - 0.809score
r-cran-extrachecks
Garrick Aden-Buie (@gadenbuie)
Prepare R packages for CRAN submission by checking for common ad-hoc requirements not caught by devtools::check().
SkillAFreshness: A★ 547Claude Code +9
Cite this page
r-testthat skill. Agent Mart. https://agentmart.dev/skills/posit-dev-r-testthat (last updated 2026-10-24)