MCP HubMCP Hub
Volver a habilidades

write-testthat-tests

pjt222
Actualizado 2 days ago
5 vistas
17
2
17
Ver en GitHub
Pruebastesting

Acerca de

Esta habilidad genera suites de pruebas completas con testthat (edición 3) para funciones de paquetes R. Cubre la organización de pruebas, expectativas, simulación, pruebas de instantánea y ayuda a alcanzar una alta cobertura de código. Úsela al agregar pruebas para nuevas funciones, aumentar la cobertura de código existente, escribir pruebas de regresión o configurar infraestructura de pruebas.

Instalación rápida

Claude Code

Recomendado
Principal
npx skills add pjt222/agent-almanac -a claude-code
Comando PluginAlternativo
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternativo
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/write-testthat-tests

Copia y pega este comando en Claude Code para instalar esta habilidad

Documentación

Write testthat Tests

Comprehensive tests for R pkg fns via testthat ed 3.

Use When

  • New pkg fns
  • Increase coverage for existing
  • Regression tests for bug fixes
  • Setup test infra for new pkg

In

  • Required: R fns to test
  • Required: Expected behavior + edge cases
  • Optional: Test fixtures|sample data
  • Optional: Target coverage % (default: 80%)

Do

Step 1: Setup Test Infra

If not done:

usethis::use_testthat(edition = 3)

Creates tests/testthat.R + tests/testthat/ dir.

Got: tests/testthat.R + tests/testthat/ dir created. DESCRIPTION has Config/testthat/edition: 3.

If err: usethis unavail → manually create tests/testthat.R w/ library(testthat); library(packagename); test_check("packagename") + add tests/testthat/.

Step 2: Test File

usethis::use_test("function_name")

Creates tests/testthat/test-function_name.R w/ template.

Got: Test file at tests/testthat/test-function_name.R w/ placeholder test_that() ready to fill.

If err: usethis::use_test() unavail → manual. Naming: test-<function_name>.R.

Step 3: Basic Tests

test_that("weighted_mean computes correct result", {
  expect_equal(weighted_mean(1:3, c(1, 1, 1)), 2)
  expect_equal(weighted_mean(c(10, 20), c(1, 3)), 17.5)
})

test_that("weighted_mean handles NA values", {
  expect_equal(weighted_mean(c(1, NA, 3), c(1, 1, 1), na.rm = TRUE), 2)
  expect_true(is.na(weighted_mean(c(1, NA, 3), c(1, 1, 1), na.rm = FALSE)))
})

test_that("weighted_mean validates input", {
  expect_error(weighted_mean("a", 1), "numeric")
  expect_error(weighted_mean(1:3, 1:2), "length")
})

Got: Basic tests cover correct out for typical inputs, NA handling, input valid err msgs.

If err: Tests fail immediately → verify fn loaded (devtools::load_all()). Err msgs don't match → regex pattern in expect_error() not exact string.

Step 4: Edge Cases

test_that("weighted_mean handles edge cases", {
  # Empty input
  expect_error(weighted_mean(numeric(0), numeric(0)))

  # Single value
  expect_equal(weighted_mean(5, 1), 5)

  # Zero weights
  expect_true(is.nan(weighted_mean(1:3, c(0, 0, 0))))

  # Very large values
  expect_equal(weighted_mean(c(1e15, 1e15), c(1, 1)), 1e15)

  # Negative weights
  expect_error(weighted_mean(1:3, c(-1, 1, 1)))
})

Got: Edge cases covered: empty, single vals, zero weights, extreme, invalid. Each w/ clear expected behavior.

If err: Fn doesn't handle edge case as expected → fix fn or adjust test. Doc intended behavior for ambiguous.

Step 5: Fixtures for Complex

Create tests/testthat/fixtures/ for test data:

# tests/testthat/helper.R (loaded automatically)
create_test_data <- function() {
  data.frame(
    x = c(1, 2, 3, NA, 5),
    group = c("a", "a", "b", "b", "b")
  )
}
# In test file
test_that("process_data works with grouped data", {
  test_data <- create_test_data()
  result <- process_data(test_data)
  expect_s3_class(result, "data.frame")
  expect_equal(nrow(result), 2)
})

Got: Fixtures provide consistent test data across files. Helpers in tests/testthat/helper.R loaded auto by testthat.

If err: Helpers not found → ensure file helper.R (not helpers.R) + located in tests/testthat/. Restart R sess if needed.

Step 6: Mock External Deps

test_that("fetch_data handles API errors", {
  local_mocked_bindings(
    api_call = function(...) stop("Connection refused")
  )
  expect_error(fetch_data("endpoint"), "Connection refused")
})

test_that("fetch_data returns parsed data", {
  local_mocked_bindings(
    api_call = function(...) list(data = list(value = 42))
  )
  result <- fetch_data("endpoint")
  expect_equal(result$value, 42)
})

Got: External deps (APIs, DBs, net calls) mocked → tests run w/o real connections. Mock returns exercise data processing logic.

If err: local_mocked_bindings() fails → ensure mocked fn accessible in test scope. Other pkgs → use .package arg.

Step 7: Snapshot Tests for Complex Out

test_that("format_report produces expected output", {
  expect_snapshot(format_report(test_data))
})

test_that("plot_results creates expected plot", {
  expect_snapshot_file(
    save_plot(plot_results(test_data), "test-plot.png"),
    "expected-plot.png"
  )
})

Got: Snapshot files created in tests/testthat/_snaps/. First run creates baseline; subsequent compare.

If err: Snapshots fail after intentional change → update w/ testthat::snapshot_accept(). Cross-platform diffs → variant param to maintain platform-specific.

Step 8: Skip Conditions

test_that("database query works", {
  skip_on_cran()
  skip_if_not(has_db_connection(), "No database available")

  result <- query_db("SELECT 1")
  expect_equal(result[[1]], 1)
})

test_that("parallel computation works", {
  skip_on_os("windows")
  skip_if(parallel::detectCores() < 2, "Need multiple cores")

  result <- parallel_compute(1:100)
  expect_length(result, 100)
})

Got: Tests requiring special env (net, DB, multi cores) guarded w/ skip. Run locally, skip on CRAN|restricted CI.

If err: Tests fail on CRAN|CI but pass locally → add skip_on_cran(), skip_on_os(), skip_if_not() at top of test_that().

Step 9: Run + Coverage

# Run all tests
devtools::test()

# Run specific test file
devtools::test_active_file()  # in RStudio
testthat::test_file("tests/testthat/test-function_name.R")

# Check coverage
covr::package_coverage()
covr::report()

Got: All tests pass devtools::test(). Coverage report shows target met (aim > 80%).

If err: Tests fail → read out for assertion failures. Coverage below target → covr::report() to ID untested paths + add tests.

Check

  • All tests pass devtools::test()
  • Coverage > target %
  • Every exported fn has 1+ test
  • Err conditions tested
  • Edge cases covered (NA, NULL, empty, boundary)
  • No tests depend on external state|order

Traps

  • Tests depend on each other: Each test_that() independent
  • Hardcoded paths: testthat::test_path() for fixtures
  • Float compare: expect_equal() (tolerance) not expect_identical()
  • Test private fns: Test through public API. ::: sparingly.
  • Snapshot in CI: Platform-sensitive. variant for cross-platform.
  • Forget skip_on_cran(): Net|DB|long runtime → skip on CRAN

Examples

# Pattern: test file mirrors R/ file
# R/weighted_mean.R -> tests/testthat/test-weighted_mean.R

# Pattern: descriptive test names
test_that("weighted_mean returns NA when na.rm = FALSE and input contains NA", {
  result <- weighted_mean(c(1, NA), c(1, 1), na.rm = FALSE)
  expect_true(is.na(result))
})

# Pattern: testing warnings
test_that("deprecated_function emits deprecation warning", {
  expect_warning(deprecated_function(), "deprecated")
})

  • create-r-package — setup test infra as part of pkg creation
  • write-roxygen-docs — doc fns you test
  • setup-github-actions-ci — auto run tests on push
  • submit-to-cran — CRAN requires tests pass on all platforms

Repositorio GitHub

pjt222/agent-almanac
Ruta: i18n/caveman-ultra/skills/write-testthat-tests
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Habilidades relacionadas

evaluating-llms-harness

Pruebas

Esta Skill de Claude ejecuta el benchmark lm-evaluation-harness para evaluar modelos de lenguaje en más de 60 tareas académicas estandarizadas como MMLU y GSM8K. Está diseñada para que los desarrolladores comparen la calidad de los modelos, realicen seguimiento del progreso del entrenamiento o reporten resultados académicos. La herramienta admite varios backends, incluidos modelos de HuggingFace y vLLM.

Ver habilidad

cloudflare-cron-triggers

Pruebas

Esta habilidad proporciona conocimiento integral para implementar Cron Triggers de Cloudflare y programar Workers mediante expresiones cron. Cubre la configuración de tareas periódicas, trabajos de mantenimiento y flujos de trabajo automatizados, manejando problemas comunes como expresiones cron inválidas y inconvenientes de zonas horarias. Los desarrolladores pueden utilizarla para configurar manejadores programados, probar activadores cron e integrar con Workflows y Green Compute.

Ver habilidad

webapp-testing

Pruebas

Esta habilidad de Claude proporciona un kit de herramientas basado en Playwright para probar aplicaciones web locales mediante scripts de Python. Permite verificación de frontend, depuración de interfaz de usuario, captura de pantallas y visualización de registros, mientras gestiona los ciclos de vida del servidor. Úsela para tareas de automatización de navegadores, pero ejecute los scripts directamente en lugar de leer su código fuente para evitar contaminación del contexto.

Ver habilidad

finishing-a-development-branch

Pruebas

Esta habilidad ayuda a los desarrolladores a completar el trabajo terminado verificando que las pruebas pasen y luego presentando opciones estructuradas de integración. Guía el flujo de trabajo para fusionar, crear PRs o limpiar ramas después de que se completa la implementación. Úsala cuando tu código esté listo y probado para finalizar sistemáticamente el proceso de desarrollo.

Ver habilidad