/skills:tdd-workflowfrom skills
This skill should be used when writing or reviewing tests for R packages using testthat. Covers TDD workflow, test file organization, coverage targets, and common test patterns.
Ships in skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Write a failing test first, then implement the minimal code to make it pass, then refactor. Never write implementation code without a failing test driving it.
Tests for R/{name}.R go in tests/testthat/test-{name}.R. Within the correct test file for the source file under test, place new tests adjacent to existing tests for the same function being tested, or at the end of the file if no tests for that function exist yet.
# Full suite
devtools::test(reporter = "check")
# Single file
devtools::test(filter = "name", reporter = "check")
Testing functions load code automatically. You do not need to call library() or devtools::load_all() separately.
Goal: 100% for every edited file. After editing R/{name}.R, replace {name} with the actual filename and verify:
# Replace {name} with the actual filename (e.g., "plot_ftir")
covr_res <- devtools:::test_coverage_active_file("R/{name}.R")
which(purrr::map_int(covr_res, "value") == 0)
Alternatively refer to the available skill test-coverage for more testing coverage instructions.
If a specific line or branch cannot be covered without unreasonable test contortion (e.g., a defensive check against an impossible internal state), add a # nocov start / # nocov end comment pair around those lines and leave a comment explaining why coverage is excluded. Do not write tests purely to hit a coverage number.
Files excluded from the coverage requirement:
R/*-package.RR/aaa-shared_params.RR/import-standalone-*.RAfter editing R/{name}.R, verify coverage with this checklist:
devtools::test(reporter = "check") to confirm all tests passcovr_res <- devtools:::test_coverage_active_file("R/{name}.R")which(purrr::map_int(covr_res, "value") == 0)# nocov start / # nocov end with explanationTest individual functions in isolation:
test_that("fetch_records() returns a tibble (#2)", {
result <- fetch_records(sample_input)
expect_s3_class(result, "tbl_df")
})
Test end-to-end pipelines through multiple functions:
test_that("build_report() produces expected output (#15)", {
input <- data.frame(value = c(1.123, 2.456, NA))
result <- build_report(input, tempfile())
expect_equal(nrow(result), 2L)
})
For complex outputs that are hard to specify with equality assertions:
test_that("build_summary print method is stable (#123)", {
expect_snapshot(print(build_summary(sample_data)))
})
When snapshots change intentionally, read the full diff of the .md snapshot file to confirm the change matches the expected behavioral change described in the issue. Only call testthat::snapshot_accept() after confirming the new snapshot output is correct. Never accept a snapshot diff without reviewing it:
testthat::snapshot_accept("test_name")
Snapshots are stored in tests/testthat/_snaps/. The filename corresponds to the R file being tested, ending with .md.
desc of every new test_that() call should end with one or more parenthetical issue references for the issue(s) verified by those tests — typically the issue currently being solved. Never guess or invent issue numbers. Determine the number from the user's prompt, the branch name (git branch --show-current), or gh issue list. Before writing a number, verify you can trace it to one of these sources. If none of these sources yields a verifiable issue number (e.g., gh is unavailable or returns an error, the branch name contains no number, and the user's prompt does not mention one), use #noissue and inform the user that an issue reference could not be determined automatically. If no tracked issue applies, do not add a reference number. The numbers in the examples below are illustrative placeholders — do not copy them:
test_that("fetch_records() returns correct columns (#1)", { ... })
test_that("build_summary() returns correct columns (#2, #3)", { ... })
test_that(".check_record() errors on empty input", { ... })
Refer to references/deprecated_patterns.md for the complete list of deprecated patterns and their modern equivalents.
expect_equal(x, y) # with numeric tolerance
expect_equal(x, y, tolerance = 0.001)
expect_equal(x, y, ignore_attr = TRUE)
expect_identical(x, y) # exact match
Errors thrown by this package (via .cli_abort()) should always be tested
with expect_error()
test_that("process_data() errors on empty input (#42)", {
expect_error(
process_data(data.frame()),
"PlotFTIR",
"empty_input"
)
})
Errors from other packages can be tested with expect_error(), optionally
wrapped in expect_snapshot() to lock down the message text:
expect_error(code, "pattern")
expect_error(code, class = "some-error-class")
# Lock down both class and message text:
test_that("fetch_records errors on invalid input (#456)", {
expect_snapshot(
(expect_error(
fetch_records("not valid input"),
class = "pkg-error"
))
)
})
expect_warning(code)
expect_no_warning(code)
expect_message(code)
expect_no_message(code)
expect_setequal(x, y) # same elements, any order
expect_in(element, set)
expect_named(x, c("a", "b"))
expect_type(x, "double")
expect_s3_class(x, "tbl_df")
expect_length(x, 10)
expect_null(x)
These expectations are a last resort when more-specific checks aren't available.
expect_true(x)
expect_false(x)
withr patterns for temporary stateAny state modification must use withr::local_* patterns, as below, as to not affect user settings, environment, or persist files.
withr::local_options(list(PlotFTIR.verbose = TRUE))
withr::local_envvar(MY_VAR = "value")
withr::local_tempfile(lines = c("a", "b"))
Store static test data in tests/testthat/fixtures/ and access via:
test_path("fixtures", "sample.rds")
Mock functions that return non-deterministic output (e.g. random numbers, current timestamps, UUIDs) or that depend on external state (file system, environment variables, system time). Do not mock stable, pure functions within this package.
After editing R/{name}.R, verify coverage with this checklist:
devtools::test(reporter = "check") to confirm all tests passtest-coverage skill to ensure new/modified lines meet target coverage thresholds before finalizing.# nocov start / # nocov end with explanationFiles excluded from coverage requirement:
R/*-package.RR/aaa-shared_params.RR/import-standalone-*.RWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub pbulsink/skills --plugin skills16 skills in this plugin
/agent-md-refactorRefactor bloated AGENTS.md, CLAUDE.md, or similar agent instruction files to follow progressive disclosure principles. Splits monolithic files into organized, linked documentation./app-screenshotThis skill should be used when capturing screenshots of R Shiny apps for documentation — vignettes, READMEs, and app tours. Covers driving a live app with shinytest2's AppDriver, isolating demo state from real data, and producing numbered PNGs ready to embed./documentThis skill should be used when documentation for R package functions is requested or required.