R-

R Testing Defaults

R testing conventions using testthat for unit testing and test structure

R
Testing
Default

Details

Language / Topic
rR
Category
Testing

Rules

balanced
- Use `testthat` — place test files in `tests/testthat/` named `test-<function>.R` mirroring the source file they test.
- Use `expect_equal()` for value comparisons, `expect_error()` for error assertions, and `expect_warning()` for warning assertions.
- Run tests with `devtools::test()` locally and `R CMD check` in CI to also run examples and vignettes.
- Use `test_that("description", { ... })` blocks to group related expectations and produce descriptive failure messages.
- Use `expect_snapshot()` to capture and lock in complex output, error messages, or printed summaries — update snapshots with `snapshot_accept()`.
- Use `withr::local_*` helpers inside `test_that` blocks to set temporary options, environment variables, or working directories.
- Use `mockery::mock()` or `testthat::local_mocked_bindings()` to stub external function calls in unit tests.