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.
In this repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rust-async-reviewSkill
- rust-borrow-checkerSkill
- rust-crates-publishSkill
- rust-dependency-auditSkill
- rust-error-handlingSkill
- rust-lint-ciSkill
- rust-unsafe-reviewSkill
- rust-workspace-setupSkill
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
- Very specific: line 72
Test private functions from the same module's tests submodule (use super::*).
≈ “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.
Files
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 benchfails (criterion bench withoutharness = 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
- Inventory (read-only):
Fixpython3 scripts/test_inventory.py . python3 scripts/test_inventory.py . --jsonhighfindings first (benches that cannot run). - Decide what to test per public item, using
references/patterns.md:
- pure logic -> unit tests in
#[cfg(test)] mod testsnext to the code - public API and cross-module flows ->
tests/*.rs(compiled as separate crates) - usage examples -> doc tests (they run with
cargo test --docfor libraries only) - parsers, encoders, data structures -> proptest round-trip / invariant properties
- large outputs (rendered text, JSON, CLI output) -> insta snapshots
- pure logic -> unit tests in
- 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 withouttempfile). - 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 - Benchmarks (only when performance matters): criterion or divan in
benches/, a[[bench]]entry withharness = false,std::hint::black_boxaround inputs, and a saved baseline:
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.cargo bench --bench parse -- --save-baseline before # change the code cargo bench --bench parse -- --baseline before - Re-run the inventory and report counts before and after.
Decision points
- Test private functions from the same module's
testssubmodule (use super::*); do not make thingspubfor tests. - Flaky because of time:
#[tokio::test(start_paused = true)]andtokio::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 withcargo test -- --ignored. #[should_panic]always withexpected = "..."; prefer returningResultand 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% linesDoes 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
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.

