agentleFS
botirkhaltaevbotirkhaltaev/botir-skills/skills/rust-testing-benchmarking/SKILL.md

rust-testing-benchmarking

Skill4 starsChanged 4 months agoApache-2.0View on GitHub

Master Rust testing and benchmarking. Use when writing unit tests, integration tests, documentation tests, property-based tests with proptest, statistical benchmarking with Criterion.rs, or setting up test infrastructure.

The file

Files
SKILL.md

Rust Testing & Benchmarking

Comprehensive guide to testing and benchmarking in Rust.

When to Use This Skill

  • Writing unit tests with #[test]
  • Creating integration tests
  • Writing documentation tests (doctests)
  • Setting up property-based tests with proptest
  • Benchmarking with Criterion.rs
  • Testing async code

Test Organization

Unit Tests

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    #[should_panic(expected = "divide by zero")]
    fn test_panic() {
        panic!("divide by zero");
    }

    #[test]
    fn result_test() -> Result<(), String> {
        if true {
            Ok(())
        } else {
            Err("failed".to_string())
        }
    }
}

Integration Tests

// tests/integration_test.rs
use my_crate::add;

#[test]
fn test_integration() {
    assert_eq!(add(2, 2), 4);
}

Doc Tests

/// Adds two numbers.
///
/// # Examples
///
/// ```
/// assert_eq!(add(2, 2), 4);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

Run with: cargo test --doc

Property-Based Testing with proptest

Setup

[dev-dependencies]
proptest = "1.0"

Basic Usage

use proptest::prelude::*;

proptest! {
    #[test]
    fn test_addition(a in 0..1000i32, b in 0..1000i32) {
        assert!(a + b >= a);
        assert!(a + b >= b);
    }
}

Custom Strategies

use proptest::strategy::Strategy;

fn non_empty_string() -> impl Strategy<Value = String> {
    "[a-z]{1,100}"
}

proptest! {
    #[test]
    fn test_string(s in non_empty_string()) {
        assert!(!s.is_empty());
    }
}

Benchmarking with Criterion.rs

Setup

[dev-dependencies]
criterion = "0.5"

[[bench]]
name = "my_benchmark"
harness = false

Basic Benchmark

use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}

fn bench_fibonacci(c: &mut Criterion) {
    c.bench_function("fibonacci 20", |b| {
        b.iter(|| fibonacci(black_box(20)))
    });
}

criterion_group!(benches, bench_fibonacci);
criterion_main!(benches);

Run with: cargo bench

Benchmark with Inputs

fn bench_vec_sort(c: &mut Criterion) {
    let mut group = c.benchmark_group("sort");
    
    for size in [100, 1000, 10000].iter() {
        let mut vec: Vec<i32> = (0..*size).collect();
        
        group.bench_with_input(format!("{}", size), &size, |b, &s| {
            b.iter(|| {
                let mut v = vec.clone();
                v.sort();
            })
        });
    }
    
    group.finish();
}

Async Benchmarks

fn bench_async(c: &mut Criterion) {
    let rt = tokio::runtime::Runtime::new().unwrap();
    
    c.bench_function("async_task", |b| {
        b.to_async(&rt).iter(|| async {
            some_async_function().await
        })
    });
}

Reference Map

  • references/unit-tests.md - Unit test patterns
  • references/integration-tests.md - Integration test setup
  • references/property-testing.md - proptest strategies
  • references/benchmarking.md - Criterion.rs setup

Key Commands

cargo test
cargo test --test integration_test
cargo test --doc
cargo bench
cargo test --release

Key References

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.