agentleFS
gurinderugurinderu/craft/skills/rust-testing/SKILL.md

rust-testing

Skill1 starsChanged 40 days agoMITView on GitHub

Rust testing — unit, integration, async, table-driven (rstest), property-based (proptest), fuzzing (cargo-fuzz), mutation (cargo-mutants, over the crate or only a change's diff with --in-diff), mocking (mockall), snapshot (insta), doc tests, and coverage. Use when writing or reviewing Rust tests, adding coverage, choosing a testing crate, or setting up CI. Triggers: #[tokio::test], rstest, proptest, cargo-fuzz, cargo-mutants, mutants on the diff, --in-diff, mockall, insta, nextest.

44 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use rust-api-design, rust-async-patterns and 4 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • adxptivedSkill

    rust-api-design

    Rust API Design guidelines, naming conventions, and best practices. Use when: - User is designing a library, crate, or public APIs - Implementing patterns like Builder, Typestate, or Extension traits - Designing public structures, enums, or traits - Working with common traits (From/Into, Default, AsRef, Debug, Clone) - Applying #[must_use] or #[non_exhaustive] attributes - Restricting trait implementation via Sealed Traits - Formatting naming structures and lifetime designs

    Also in 3 projects that use this

  • klauswgSkill

    rust-async-patterns

    Master Rust async programming with Tokio, async traits, error handling, and concurrent patterns. Use when building async Rust applications, implementing concurrent systems, or debugging async code.

    Also in 5 projects that use this

  • MicrosoftSkill

    rust-best-practices

    Guide for writing idiomatic Rust code based on Apollo GraphQL's best practices handbook. Use this skill when: (1) writing new Rust code or functions, (2) reviewing or refactoring existing Rust code, (3) deciding between borrowing vs cloning or ownership patterns, (4) implementing error handling with Result types, (5) optimizing Rust code for performance, (6) writing tests or documentation for Rust projects.

    Also in 9 projects that use this

  • junimnjwSkill

    rust-patterns

    Idiomatic Rust patterns, ownership, error handling, traits, concurrency, and best practices for building safe, performant applications.

    Also in 4 projects that use this

  • GoogleSkill

    improve-codebase-architecture

    Scan a codebase for deepening opportunities, present them as a visual HTML report, then grill through whichever one you pick.

    Also in 3 projects that use this

  • GoogleSkill

    grill-me

    A relentless interview to sharpen a plan or design.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

6 rules. 1 is common, 0 are distinctive, and 5 are very specific to this project.

  • 1

    Common

    Most of the 43 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Common:

    Test behavior, not implementation details.

    line 91
  2. Very specific:

    Prefer assert_eq! over assert!(a == b) — better failure messages.

    line 92
  3. Very specific:

    Put unit tests in a #[cfg(test)] mod tests next to the code.

    line 94
  4. Very specific:

    Use #[should_panic] when you could assert result.is_err() (panic tests can pass for the wrong reason).

    line 98
  5. Very specific:

    Use thread::sleep to "wait" in async tests — use tokio::time::pause/advance or channels.

    line 100
  6. Very specific:

    Test crates are [dev-dependencies] — they never ship in the final binary.

    line 112

“Common” is measured: kept by at least 6 in 10 of the 43 edited versions of this file we could read. “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

Self-contained guide to testing Rust: what to reach for, the TDD loop in Rust terms, and the commands. Deep examples live in the sub-files below — read the one you need, not all three.

When to Use

  • Writing tests for new functions, methods, traits, or modules
  • Adding coverage to existing code
  • Choosing the right testing crate for a case (table-driven, property, mock, snapshot)
  • Setting up the test runner and CI for a Rust project

Pick the right tool

SituationReach forWhere
One behavior, one inputplain #[test]core.md
Function returns Result / can panic? in test / #[should_panic]core.md
Cross-crate / public API behaviorintegration test in tests/core.md
Show usage in the docsdoc test (``` in ///)core.md
Same logic, many input/output pairsrstest #[case]advanced.md
Shared setup across testsrstest #[fixture]advanced.md
async fn under test#[tokio::test]advanced.md
Invariant must hold for all inputsproptestadvanced.md
Isolate a unit from a trait dependencymockalladvanced.md
Behavior spec in Given/When/Then (or Gherkin .feature)plain test, or cucumberadvanced.md
Assert on large/structured outputinsta snapshottooling.md
Faster runs, better output, CIcargo nextesttooling.md
Prove what's testedcargo llvm-covtooling.md
Find crashes/panics on adversarial bytes (parser, untrusted input, unsafe)cargo-fuzzadvanced.md
Check tests actually catch bugs, not just runcargo-mutantstooling.md
Check a change's tests catch its own breakagecargo mutants --in-diff <diff file>tooling.md

Benchmarking (criterion, cargo bench) is not testing — it belongs to rust-performance, not here.

The TDD loop in Rust

Discipline is RED → GREEN → REFACTOR. In Rust the mechanics are:

// RED — write the test first; stub the impl with todo!() so it compiles but fails
pub fn slugify(s: &str) -> String { todo!() }

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn lowercases_and_dashes() {
        assert_eq!(slugify("Hello World"), "hello-world");
    }
}
// `cargo test` → panics at "not yet implemented" = RED
// GREEN — minimal code to pass
pub fn slugify(s: &str) -> String {
    s.to_lowercase().replace(' ', "-")
}
// `cargo test` → PASS. Now REFACTOR with the test as a safety net.

Rules that keep the loop honest:

  • Watch the test fail first — a test that was never red proves nothing.
  • One behavior per test; the name states the scenario (rejects_empty_input, not test1).
  • Tests must be independent — no shared mutable state, no ordering assumptions.

Command cheatsheet

cargo test                     # all tests (unit + integration + doc)
cargo test name_substring      # only tests whose name matches
cargo test -- --nocapture      # show println!/dbg! output
cargo test --lib               # unit tests only
cargo test --test api          # one integration binary (tests/api.rs)
cargo test --doc               # doc tests only
cargo test -- --include-ignored  # also run #[ignore] tests
cargo test --no-fail-fast      # don't stop at first failure

(For cargo nextest and coverage, see tooling.md.)

DO / DON'T

DO

  • Test behavior, not implementation details.
  • Prefer assert_eq! over assert!(a == b) — better failure messages.
  • Return Result from tests and use ? instead of unwrapping each step.
  • Put unit tests in a #[cfg(test)] mod tests next to the code; integration tests in tests/.
  • Quarantine or fix flaky tests immediately — never #[ignore] and forget.

DON'T

  • Use #[should_panic] when you could assert result.is_err() (panic tests can pass for the wrong reason).
  • Mock everything — prefer a real or in-memory implementation when feasible; mock only at true boundaries.
  • Use thread::sleep to "wait" in async tests — use tokio::time::pause/advance or channels.
  • Assert on debug-formatted blobs by hand when a snapshot (insta) is clearer.

Project defaults

This collection targets the latest stable toolchain:

[package]
edition = "2024"

Test crates are [dev-dependencies] — they never ship in the final binary.

Boundaries

This skill covers writing and running Rust tests. Adjacent concerns belong elsewhere:

  • Benchmarking and profiling (criterion, cargo bench, flamegraphs) → rust-performance.
  • Fake-port unit tests and hexagonal adapter layering (what to fake vs. integrate) → rust-architecture.
  • Deriving BDD scenarios from requirements → specs.
  • Reporting and owning skipped-test status (don't let "no Docker" pass as "covered").
  • Flagging weak or missing tests in a diff → rust-review.

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.