The ensure package provides an addin for drafting testthat unit testing code using LLMs. Triggering the addin will open a corresponding test file and begin writing tests into it. The ensurer is familiar with testthat 3e as well as tidy style, and incorporates context from the rest of your R package to write concise and relevant tests.
You can install ensure like so:
pak::pak("simonpcouch/ensure")Then, make sure that you have an
ANTHROPIC_API_KEY environment
variable set, and you’re ready to go. If you’d like to use an LLM other
than Anthropic’s Claude 3.5 Sonnet—like OpenAI’s ChatGPT or a local
ollama model—to power the ensurer, set the .ensure_fn and
.ensure_args options in your .Rprofile, like
options(.ensure_fn = "chat_openai") to use OpenAI’s GPT-4o or
options(.ensure_fn = "chat_openai", .ensure_args = list(model = "gpt-4o-mini"))
to use their GPT-4o-mini model.
The ensurer is interfaced with the via the RStudio addin “ensure: Test R
code.” For easiest access, we recommend registering the ensure addin to
a keyboard shortcut. In RStudio, navigate to
Tools > Modify Keyboard Shortcuts > Search "ensure"—we suggest
Ctrl+Alt+T (or Ctrl+Cmd+T on macOS). The ensurer is currently not
available in Positron as Positron has yet to implement document ids
that the package needs to toggle between source and test files.
Once those steps are completed, you’re ready to use the ensure addin with a keyboard shortcut.
To use the ensurer, just trigger the addin (optionally selecting some code to only write tests for a certain portion of the file) and watch your testing code be written.
