rust-tests-guidelines
Follow best practices for writing maintainable and robust Rust tests for public and private APIs.
Install
mkdir -p .claude/skills/rust-tests-guidelines && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/1061" && unzip -o skill.zip -d .claude/skills/rust-tests-guidelines && rm skill.zipInstalls to .claude/skills/rust-tests-guidelines
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Guidelines for writing Rust tests. Use this when you want to write Rust tests.Key capabilities
- →Test against public APIs
- →Implement snapshot testing with insta
- →Add property-based tests with proptest
- →Co-locate private API tests in cfg(test) modules
How it works
It organizes tests into a dedicated tests directory for public APIs and uses conditional compilation for private API testing. It mandates the use of specialized crates for complex output comparisons and invariant testing.
Inputs & outputs
When to use rust-tests-guidelines
- →Writing unit tests for Rust public APIs
- →Setting up property-based tests
- →Implementing snapshot testing for complex output
- →Structuring test suites for crates
About this skill
Guidelines for Writing Rust Tests
Guidelines for writing new tests for Rust code.
Guidelines
- Test against the public API of the code under test.
- Test private APIs if and only if the private component is highly complex and difficult to test through the public API.
- Use
instawhenever you are testing output that is difficult to predict or compare. - Test a behavior that holds over a range of inputs, such as a conversion, a bound, an ordering or a round trip, with a property test over generated inputs, not with a few hand-picked values. Keep example tests for boundaries and regressions. One property holds for every type: any value its constructors or parsers accept survives every method without panicking. A NaN that passes validation is such a value.
- Testing code should be written with the same care reserved to production code. Avoid unnecessary duplication, introduce helpers to reduce boilerplate and ensure readability. The intent of a test should be obvious or, if not possible, clearly documented.
- Do not reference exact line numbers in comments, as they may change over time.
- Test each fact once. A test named for one behavior asserts that behavior, not a mix of parsing, defaults and edge cases.
- For tables of cases, name each case: a slice of structs or
(name, input, expected)tuples, not parallel arrays, bare booleans, orifbranches inside the shared loop. - Test the code, not the language or its dependencies: not
thiserror'sDisplay, enum discriminants, or float casts. - Do not pin a bug or a panic on user input with a test. Fix it, or leave it out and say so in the PR.
- Name a test for the behavior it asserts (
nan_observations_are_ignored), never for the artifact it belongs to (contract,boundary,compat). Let the name and the code say what is tested; add one short comment only where neither makes the behavior plain. A test whose behavior needs more than a sentence to describe tests several things: split it. - Do not write leak- or double-free-counting allocators or similar instrumentation for tests Miri runs: it already catches leaks, double frees and use-after-free in them. Only a test that cannot run under Miri, such as one calling into C, may count allocations to check ownership across the boundary.
Code organization
- Put tests for public (
pub) items under the crate'stestsdirectory. Two layouts are in use and both are fine — match whichever the crate already has:tests/integration/— one test crate with its ownmain.rsand a module per area (trie_rs,geo,query_eval, …). Prefer this for a new crate: it compiles as a single unit instead of one binary per file.- Cargo's default layout — one integration binary per
tests/*.rsfile (varint,fork_gc,rlookup, …).
- If the test must rely on private APIs, co-locate it with the code it tests, using a
#[cfg(test)]module. Integration tests cannot reachpub(crate)or private items, so this is the only option for them — but prefer exercising the behavior through the public API where you can, per guideline 2 above. - Making an item
pubsolely so integration tests can reach it is acceptable. We own both the crate and its callers, so visibility hygiene is not worth a review round.
Dealing with extern C symbols
Check out CONTRIBUTING.md for instructions on how to deal with undefined C symbols in Rust tests.
When not to use it
- →When testing private APIs that are simple enough to cover via public interfaces
Limitations
- →Requires specific handling for undefined C symbols
- →Avoids referencing exact line numbers in comments
How it compares
It enforces a strict separation between public API testing and internal implementation testing compared to standard ad-hoc testing.
Compared to similar skills
rust-tests-guidelines side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| rust-tests-guidelines (this skill) | 7 | 9mo | No flags | Beginner |
| check-rust-coverage | 5 | 9mo | Review | Beginner |
| check-code-quality | 1 | 4mo | Review | Advanced |
| lint | 1 | 5mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by RediSearch
View all by RediSearch →You might also like
check-rust-coverage
RediSearch
Check which Rust lines are not covered by Rust tests.
check-code-quality
r3bl-org
Run comprehensive Rust code quality checks including compilation, linting, documentation, and tests. Use after completing code changes and before creating commits.
lint
RediSearch
Check code quality and formatting before committing changes
run-clippy
r3bl-org
Run clippy linting, enforce comment punctuation rules, format code with cargo fmt, and verify module organization patterns. Use after code changes and before creating commits.
move-code-quality
davila7
Analyzes Move language packages against the official Move Book Code Quality Checklist. Use this skill when reviewing Move code, checking Move 2024 Edition compliance, or analyzing Move packages for best practices. Activates automatically when working with .move files or Move.toml manifests.
pre_commit
pums974
Standardized pre-commit workflow for linting, formatting, and local quality gates.