|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## About This Project |
| 6 | + |
| 7 | +testthat is R's most popular unit testing framework, used by thousands of CRAN packages. It provides functions to make testing R code as fun and addictive as possible, with clear expectations, visual progress indicators, and seamless integration with R package development workflows. |
| 8 | + |
| 9 | +## Key Development Commands |
| 10 | + |
| 11 | +### Testing |
| 12 | +- `devtools::test()` or `Ctrl/Cmd+Shift+T` in RStudio - Run all tests |
| 13 | +- `devtools::test_file("tests/testthat/test-filename.R")` - Run tests in a specific file |
| 14 | +- `testthat::test_local()` - Run tests for local source package |
| 15 | +- `testthat::test_package("testthat")` - Run tests for installed package |
| 16 | +- `R CMD check` - Full package check including tests |
| 17 | + |
| 18 | +### Building and Installation |
| 19 | +- `devtools::load_all()` or `Ctrl/Cmd+Shift+L` - Load package for development |
| 20 | +- `devtools::document()` - Generate documentation |
| 21 | +- `devtools::check()` - Run R CMD check |
| 22 | +- `devtools::install()` - Install package locally |
| 23 | + |
| 24 | +## Core Architecture |
| 25 | + |
| 26 | +### Main Components |
| 27 | + |
| 28 | +1. **Core Testing Functions** (`R/test-that.R`, `R/test-package.R`): |
| 29 | + - `test_that()` - The fundamental testing function |
| 30 | + - `test_local()`, `test_package()`, `test_check()` - Different ways to run test suites |
| 31 | + |
| 32 | +2. **Expectations** (`R/expect-*.R`): |
| 33 | + - Modular expectation functions (equality, conditions, types, etc.) |
| 34 | + - Each expectation type has its own file following the pattern `expect-[type].R` |
| 35 | + |
| 36 | +3. **Reporters** (`R/reporter*.R`): |
| 37 | + - Different output formats for test results |
| 38 | + - Object-oriented design with base `Reporter` class |
| 39 | + - Includes check, debug, progress, summary, JUnit, TAP formats |
| 40 | + |
| 41 | +4. **Snapshot Testing** (`R/snapshot*.R`): |
| 42 | + - Value snapshots, file snapshots, output snapshots |
| 43 | + - Automatic management and comparison of expected outputs |
| 44 | + |
| 45 | +5. **Parallel Testing** (`R/parallel*.R`): |
| 46 | + - Multi-core test execution |
| 47 | + - Configuration via `Config/testthat/parallel: true` in DESCRIPTION |
| 48 | + |
| 49 | +6. **Mocking** (`R/mock*.R`, `R/mock2*.R`): |
| 50 | + - Function mocking capabilities |
| 51 | + - Both legacy (`mock.R`) and modern (`mock2*.R`) implementations |
| 52 | + |
| 53 | +### Key Design Patterns |
| 54 | + |
| 55 | +- **Editions**: testthat has different "editions" with varying behavior, controlled by `Config/testthat/edition` |
| 56 | +- **Reporters**: Extensible reporting system using R6 classes |
| 57 | +- **Lazy Evaluation**: Expectations use substitute() and lazy evaluation for better error messages |
| 58 | +- **C++ Integration**: Core functionality implemented in C++ for performance |
| 59 | + |
| 60 | +### File Organization |
| 61 | + |
| 62 | +- `R/` - All R source code, organized by functionality |
| 63 | +- `src/` - C++ source code and Makevars |
| 64 | +- `inst/include/testthat/` - C++ headers for other packages to use |
| 65 | +- `tests/testthat/` - Package's own comprehensive test suite |
| 66 | +- `vignettes/` - Documentation on testing concepts and workflows |
| 67 | + |
| 68 | +### Important Configuration |
| 69 | + |
| 70 | +The package uses several DESCRIPTION fields for configuration: |
| 71 | +- `Config/testthat/edition: 3` - Sets testthat edition |
| 72 | +- `Config/testthat/parallel: true` - Enables parallel testing |
| 73 | +- `Config/testthat/start-first` - Tests to run first in parallel mode |
| 74 | + |
| 75 | +### C++ Testing Infrastructure |
| 76 | + |
| 77 | +testthat provides C++ testing capabilities via Catch framework: |
| 78 | +- Headers in `inst/include/testthat/` |
| 79 | +- Test runner infrastructure in `src/test-runner.cpp` |
| 80 | +- Integration with R's testing system |
| 81 | + |
| 82 | +### Snapshot Testing Workflow |
| 83 | + |
| 84 | +- Snapshots stored in `tests/testthat/_snaps/` |
| 85 | +- Different snapshot types: values, files, output |
| 86 | +- Version-specific snapshots for different R versions |
| 87 | +- Use `testthat::snapshot_accept()` to update snapshots |
| 88 | + |
| 89 | +This codebase prioritizes backward compatibility, comprehensive testing, and clear, descriptive error messages to help R developers write better tests. |
0 commit comments