Back to Skills

write-unit-tests

Write minimal, concise testthat 3e unit tests for R package code

43stars4forksUpdated 2/25/2026

Security Assessment

Safe(100/100)
Security Score100/100

About write-unit-tests

The 'write-unit-tests' skill helps developers write concise and minimal unit tests for R package code using the testthat 3e framework. It takes an R file as input, with a specific header indicating the contents and selection. Based on the provided selection, the skill generates appropriate testthat unit tests that verify the functionality of the code. This skill eliminates the need to manually write boilerplate test code, making the process faster and more efficient for developers who need to ensure their code is well-tested and robust.

The primary features of this skill include generating unit tests that focus on expected outcomes, error handling, and various conditions. The tests can check for boolean conditions, expected values, error messages, warnings, and other edge cases using functions like 'expect_true()', 'expect_equal()', and 'expect_snapshot()'. The tool also supports mocking, allowing developers to simulate certain conditions (e.g., operating systems, package availability) for comprehensive testing. This skill is highly useful for R developers looking to ensure the reliability of their code with minimal manual effort.

Target users for this skill include R package developers, data scientists, and software engineers who need to write unit tests efficiently. It is especially beneficial for developers working in teams who need consistent, high-quality tests for their code. Additionally, it’s a useful tool for those looking to automate or streamline their testing process.

FAQ

How does this skill generate unit tests?

The skill generates unit tests by analyzing a given selection of R code and produces corresponding testthat unit tests that check for expected behavior, errors, and edge cases. It uses functions like 'expect_true()', 'expect_equal()', and 'expect_snapshot()' to verify the code’s functionality.

Can this skill handle error testing?

Yes, the skill can generate tests for error handling. It uses 'expect_snapshot()' with the 'error = TRUE' parameter to verify that errors are raised correctly when appropriate.

Is mocking supported in this skill?

Yes, the skill supports mocking through 'testthat::local_mocked_bindings()'. This allows developers to simulate specific conditions such as operating systems or package availability for comprehensive testing.

Does this skill support other testing frameworks?

No, this skill is specifically designed for generating unit tests using the testthat 3e framework in R. It does not support other testing frameworks.

What is the main limitation of this skill?

The skill can only generate tests for the provided selection of code. If the selection does not contain the complete context or necessary logic, the generated tests may not be comprehensive or accurate.

Install write-unit-tests

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill