agentleFS
saizulsaizul/rust-dev-skills/skills/rust-testing-benches/SKILL.md

rust-testing-benches

Skill0 starsChanged 3 days agoMITView on GitHub

Plan and improve Rust tests and benchmarks - unit tests in cfg(test) modules, integration tests in tests/, doc tests, property tests with proptest, snapshot tests with insta, parameterised tests with rstest, async tests with tokio test and paused time, cargo-nextest, coverage with cargo-llvm-cov, and benchmarks with criterion or divan (harness = false). Includes an inventory script that counts unit, integration, doc and property tests and benches per crate and flags benches missing harness = false, should_panic without expected, ignore without a reason, sleeps in tests, doc tests in binary-only crates and library crates without tests. Use when the user asks how to test a Rust crate, to add property or snapshot tests, set up criterion, measure coverage, speed up cargo test, or sees "cargo bench" fail with a duplicate main.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Test private functions from the same module's tests submodule (use super::*).

    line 72

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust testing and benchmarks

Goal: fast unit tests next to the code, integration tests for the public API, doc examples that compile, property tests for parsers and invariants, and benchmarks that are statistically meaningful before anyone claims a speed-up.

Paths below are relative to this skill's folder (in Claude Code: ${CLAUDE_SKILL_DIR}).

When to use

  • "Write tests for this crate", "what should I test?", "add property tests"
  • Setting up criterion/divan benchmarks or comparing performance before and after
  • Coverage reports, flaky tests, slow cargo test
  • cargo bench fails (criterion bench without harness = false)

When not to use

  • CI wiring for tests (matrix, caching): rust-lint-ci
  • Testing unsafe code for UB with Miri: rust-unsafe-review
  • Async design problems found while testing: rust-async-review

Workflow

  1. Inventory (read-only):
    python3 scripts/test_inventory.py .
    python3 scripts/test_inventory.py . --json
    Fix high findings first (benches that cannot run).
  2. Decide what to test per public item, using references/patterns.md:
    • pure logic -> unit tests in #[cfg(test)] mod tests next to the code
    • public API and cross-module flows -> tests/*.rs (compiled as separate crates)
    • usage examples -> doc tests (they run with cargo test --doc for libraries only)
    • parsers, encoders, data structures -> proptest round-trip / invariant properties
    • large outputs (rendered text, JSON, CLI output) -> insta snapshots
  3. Write tests with the templates in references/patterns.md. Name tests after the behaviour (rejects_empty_input), assert on specific values or error variants (assert!(matches!(err, Error::Empty))), and keep each test independent (no shared files without tempfile).
  4. Run them:
    cargo test --workspace --all-features
    cargo nextest run --workspace          # faster, per-test processes (doc tests: cargo test --doc)
    cargo llvm-cov --workspace --html      # coverage report in target/llvm-cov/html
  5. Benchmarks (only when performance matters): criterion or divan in benches/, a [[bench]] entry with harness = false, std::hint::black_box around inputs, and a saved baseline:
    cargo bench --bench parse -- --save-baseline before
    # change the code
    cargo bench --bench parse -- --baseline before
    Report the change only if criterion marks it as significant; run on a quiet machine, not in shared CI runners, for absolute numbers. Setup in references/benches.md.
  6. Re-run the inventory and report counts before and after.

Decision points

  • Test private functions from the same module's tests submodule (use super::*); do not make things pub for tests.
  • Flaky because of time: #[tokio::test(start_paused = true)] and tokio::time::advance; because of ports: bind to port 0; because of files: tempfile::tempdir().
  • Slow test suite: cargo nextest, split out #[ignore = "slow"] tests and run them in a separate CI job with cargo test -- --ignored.
  • #[should_panic] always with expected = "..."; prefer returning Result and asserting the error.

Output

crate   unit integ doc prop bench
calc       4     1   2    1     2   -> after: 11 / 3 / 5 / 3 / 2
Fixed: [[bench]] eval harness = false (cargo bench failed before)
Added: proptest round-trip for parse/format, insta snapshot for render()
Coverage (cargo llvm-cov): 61% -> 84% lines

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.