/rust:rust-testingfrom rust
Guides Rust testing with built-in #[test], integration/doctest layout, cargo-nextest, and ecosystem tools (proptest, insta, criterion, mockall, rstest).
Ships in rust. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Prefer real implementations and hand-written fakes over heavy mocking — a test wired to a mock asserts how the code calls its collaborators, not what the code does. Tests are documentation that runs: a reader should learn the intended behavior from the test name, the arrange/act/assert shape, and the doctest examples on public items. Test the public contract, not the private call graph.
${CLAUDE_SKILL_DIR}/references/layout.md Unit vs integration vs doctest mechanics, tests/
crate-per-file model, tests/common/mod.rs shared-helper pattern, binary-crate lib.rs split, doctest attributes
(no_run, should_panic, ignore, hidden # lines), assert_matches! and pretty_assertions usage${CLAUDE_SKILL_DIR}/references/property-and-snapshot.md proptest vs
quickcheck strategy authoring and shrinking, insta inline/file snapshots and cargo insta review, criterion harness
setup and std::hint::black_box${CLAUDE_SKILL_DIR}/references/mocking-and-fixtures.md mockall #[automock] vs mock!,
predicates and call counts, the trait-seam pattern for in-memory fakes, rstest #[case]/#[fixture] params,
serial_test, assert_cmd + predicates for CLI integration${CLAUDE_SKILL_DIR}/references/async-tests.md #[tokio::test] flavors, tokio::time::pause/
advance for deterministic timers, tokio-test macros; cross-references the rust skill async reference for runtime
semantics${CLAUDE_SKILL_DIR}/references/versions.md date-stamped dev-dependency versions for the
testing ecosystem crates ("as of 2026-06")Read the relevant reference before writing non-trivial tests in that area.
#[cfg(test)] mod tests block in the same file as the code under test; bring the parent into
scope with use super::*. #[cfg(test)] keeps test code (and its helpers) out of the build artifact.tests module reach its ancestors, so
testing internals is possible. Reserve this for genuinely tricky private logic; prefer driving private code through
the public API.tests/ directory, next to src/. Cargo compiles each file as its own
crate, so they reach only the public API via use my_crate::... — exactly as an external consumer would.tests/common/mod.rs, not tests/common.rs. The mod.rs form is not compiled as
a separate test crate, so it produces no spurious empty "running 0 tests" section. Import it with mod common;.main.rs a thin shell that calls into lib.rs; integration tests then exercise the library
crate, since only library crates expose items to external use./// examples on public items. They double as compiled documentation and run under
cargo test --doc. Hide setup lines with a leading # and use ```no_run for examples that must compile but not
run.assert! for booleans and assert_eq!/assert_ne! for value equality; add a trailing message argument when the
failure is not self-explanatory.assert_matches! to assert a value matches a pattern (including enum variants with bindings) — it is stable in
std as of Rust 1.96 and requires use std::assert_matches::assert_matches;.pretty_assertions as a dev-dependency and use pretty_assertions::{assert_eq, assert_ne}; in test modules when
comparing large structs or collections — it renders colored line-by-line diffs instead of one unreadable line.cargo nextest run — it builds each test binary, queries it for tests, and runs
every test in its own process in parallel, giving clean per-test isolation and output.cargo test --doc. Nextest does not run doctests, so a project that only runs nextest silently
skips every doctest. Both commands belong in the verification gate.-p <pkg>/--workspace, --lib/--test <name>, -E <filterset> for
expression filtering, and --no-capture to see test stdout/stderr.--retries <N> and stop early with --fail-fast; prefer fixing nondeterminism over
leaving retries on permanently.Default to the cheapest test double that still proves the behavior, escalating only when the boundary forces it:
HashMap-backed store behind a repository
trait). Preferred for stateful collaborators; the fake is reusable across many tests.#[automock] (or mock! for foreign/multi-trait types) only at IO or
nondeterministic boundaries: network, filesystem, clock, randomness, external services behind a trait seam.Define collaborators behind a trait so the production type and the test double are interchangeable. Assert on observable
outcomes — return values, resulting state, emitted effects — not on which methods were called in which order. Do not use
mockall Sequence/in_sequence to lock internal call order; ordering assertions make tests break on harmless
refactors. Argument matchers and call counts are acceptable when the call itself is the contract (e.g. "charge the card
exactly once").
proptest (generates and shrinks values from declarative strategies) or quickcheck
(derives generators from Arbitrary); proptest is the richer default for custom input domains.insta for snapshot testing of large or structural output (serialized data, rendered text, debug dumps). Review
and accept changes with cargo insta review; commit the approved .snap files. Snapshots make intent diffable rather
than hand-asserting every field.benches/ with criterion, set harness = false on the [[bench]] target in Cargo.toml, and wrap
benchmarked inputs/outputs in std::hint::black_box so the optimizer cannot fold the work away. Benchmarks measure;
they are not correctness tests.#[tokio::test]. Default to the current-thread flavor; use
#[tokio::test(flavor = "multi_thread", worker_threads = N)] only when the test genuinely needs real parallelism.tokio::time::pause() and tokio::time::advance(duration) instead of real sleep.
Advancing virtual time fires timers instantly and removes wall-clock flakiness from timeout and interval tests.async_trait/trait_variant), returning futures via
.returning(|| Box::pin(async { ... })). The mocking decision rule applies unchanged — mock only at IO boundaries.When writing tests:
cargo test, rstest fixtures, or a specific assertion
crate, follow that and flag any divergence once rather than rewriting wholesale.cargo nextest run, cargo test --doc) when introducing tests to a
project that lacks them.When reviewing tests:
serial_test) as
a flaky-test risk and propose the deterministic alternative.The rust skill governs language conventions (ownership, error handling, module layout, the cargo/clippy/rustfmt
gates); this skill governs test structure and the testing ecosystem. When both apply, defer to rust for how the code
under test is written and to this skill for how it is exercised. For async runtime semantics behind #[tokio::test]
(executor model, Send bounds, cancellation), consult the rust skill's async reference rather than restating it here.
#[cfg(test)] mod tests; integration tests in tests/ touch only the public API; shared helpers
in tests/common/mod.rs.cargo nextest run and cargo test --doc — doctests are never silently skipped.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub xobotyi/cc-foundry --plugin rust2 skills in this plugin
Other plugins' skills for the same job