rust-testing
Skill1 starsChanged 6 months agoMITView on GitHub
Rust testing — unit tests, integration tests, property tests, benchmarks. Use this skill when: - User writes tests with #[test], #[tokio::test], or test modules - User asks about test organization, test helpers, or fixtures - User implements property-based testing with proptest or quickcheck - User writes benchmarks with criterion - User mocks traits with mockall or uses test doubles - User asks about snapshot testing with insta - User tests async code or Tokio tasks - User asks about coverage, test isolation, or CI configuration Trigger for any Rust testing question.
- Installs packages
29 other projects use a version of this.
Adopted by
Most recently taken up by
- Rojios/everything-claude-codetheir own version, first seen Nov 8, 2026
- ahmadvahidi/skill-atlastheir own version, first seen Nov 8, 2026
- damacus/agent-skillstheir own version, first seen Nov 1, 2026
- Blushyes/reeftheir own version, first seen Nov 1, 2026
- jniltinho/macos-kvm-guitheir own version, first seen Oct 25, 2026
- renepf/claude-skillstheir own version, first seen Oct 25, 2026
Used with
Projects with it also use rust-async-patterns and rust-best-practices.
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.
klauswgSkillrust-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 4 projects that use this
MicrosoftSkillrust-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 7 projects that use this
Other versions29 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 29
- loulanyue/awesome-claude-notes★ 272same name
- Blushyes/reef★ 84same name
- Fmarzochi/EGC★ 61same name
- huiali/rust-skills★ 47same name
- Bitslix/BLXCode★ 15same name
- full-stack-skills/rust-skills★ 8same name
- gongyijie85/dsh-ecc★ 8same name
- aphrody-code/bxc★ 2same name
- damacus/agent-skills★ 2same name
- idexal/idexal-skills★ 2same name
19 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository24 other files it gives its agents.
- rust-api-designSkill
- rust-asyncSkill
- rust-axumSkill
- rust-cliSkill
- rust-cloud-nativeSkill
- rust-concurrencySkill
- rust-databaseSkill
- rust-dioxusSkill
- rust-ecosystemSkill
- rust-embeddedSkill
- rust-error-handlingSkill
- rust-fintechSkill
- rust-masterySkill
- rust-memory-optimizationSkill
- rust-mlSkill
- rust-networkingSkill
- rust-observabilitySkill
- rust-ownershipSkill
- rust-design-patternsSkill
- rust-performanceSkill
- rust-securitySkill
- rust-type-systemSkill
- rust-unsafeSkill
- rust-wasmSkill
The file
We split this skill into the rules it gives an agent
9 rules. 0 are common, 7 are distinctive, and 2 are very specific to this project.
0
Common
Most of the 28 edited versions we read keep these.
7
Distinctive
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 85
Use descriptive names that read like specifications:
- Distinctive: line 439
Always split your test blocks visually into three distinct phases for maximum readability:
- Distinctive: line 441
Act: Call the unit or method under test.
- Very specific: line 460
Use structs implementing the Drop trait to handle setup and cleanup actions, ensuring database connections, file handles, or mock servers …
- Very specific: line 516
Put public behavior and cross-module flows in tests/ integration tests.
- Distinctive: line 517
Use property tests for parsers, serializers, state machines, and invariants.
- Distinctive: line 518
Keep mocks at process or trait boundaries, not around simple domain logic.
- Distinctive: line 519
Add regression tests before fixing bugs.
- Distinctive: line 520
Run doctests for public examples that users may copy.
“Common” is measured: kept by at least 6 in 10 of the 28 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.
Files
Quick Navigation
- references/unit_integration.md
- references/mocks_property.md
Rust Testing
Testing in Rust is first-class: cargo test runs unit + integration tests, doctests are auto-run, and the ecosystem has excellent tools for every testing need.
Test Organization
// Unit tests: in the same file as the code they test
// src/user.rs
pub fn validate_age(age: u32) -> bool {
age >= 0 && age <= 150
}
#[cfg(test)] // Only compiled when running tests
mod tests {
use super::*; // Access private functions
#[test]
fn valid_age_returns_true() {
assert!(validate_age(25));
}
#[test]
fn age_zero_is_valid() {
assert!(validate_age(0));
}
#[test]
fn age_over_150_is_invalid() {
assert!(!validate_age(151));
}
}// Integration tests: in tests/ directory (public API only)
tests/
├── integration_test.rs
└── common/
└── mod.rs // Shared test helpers// tests/integration_test.rs
use my_lib::UserService;
mod common; // tests/common/mod.rs
#[test]
fn create_and_find_user() {
let db = common::setup_test_db();
let svc = UserService::new(db);
let user = svc.create("Alice", "alice@example.com").unwrap();
let found = svc.find(user.id).unwrap();
assert_eq!(found.name, "Alice");
}Test Naming
Use descriptive names that read like specifications:
// Bad: no context
#[test] fn test1() {}
#[test] fn test_process() {}
// Good: <what>_<when/given>_<expected result>
#[test] fn validate_email_with_missing_at_sign_returns_error() {}
#[test] fn transfer_funds_when_balance_insufficient_returns_insufficient_funds_error() {}
#[test] fn parse_config_with_missing_required_field_fails() {}Assertions
// Basic
assert!(condition);
assert_eq!(left, right);
assert_ne!(left, right);
// With messages
assert!(user.is_active(), "Expected user {id} to be active");
assert_eq!(result, expected, "Processing {input} failed");
// Approximate equality for floats
assert!((result - 3.14159).abs() < 1e-5);
// Pattern matching
assert!(matches!(result, Ok(User { name, .. }) if name == "Alice"));
// Panics assertion
#[test]
#[should_panic(expected = "out of bounds")]
fn access_out_of_bounds_panics() {
let v = vec![1, 2, 3];
let _ = v[10];
}Arrange-Act-Assert Pattern
#[test]
fn deduct_items_from_inventory_reduces_stock() {
// Arrange
let mut inventory = Inventory::new();
inventory.add_stock("widget", 10);
// Act
let result = inventory.deduct("widget", 3);
// Assert
assert!(result.is_ok());
assert_eq!(inventory.stock("widget"), 7);
}Async Tests
// Test async functions with tokio
#[tokio::test]
async fn fetch_user_returns_correct_data() {
let client = TestClient::new();
let user = client.get_user(42).await.unwrap();
assert_eq!(user.id, 42);
}
// Single-threaded async test (faster, no thread synchronization)
#[tokio::test(flavor = "current_thread")]
async fn concurrent_tasks_complete() {
let (r1, r2) = tokio::join!(task_one(), task_two());
assert!(r1.is_ok());
assert!(r2.is_ok());
}
// Test with timeout
#[tokio::test]
async fn operation_completes_within_timeout() {
let result = tokio::time::timeout(
std::time::Duration::from_secs(1),
some_async_operation(),
)
.await;
assert!(result.is_ok(), "Operation timed out");
}Fixtures and Test Helpers
// Reusable test data builders
struct UserBuilder {
name: String,
email: String,
age: u32,
}
impl Default for UserBuilder {
fn default() -> Self {
Self {
name: "Test User".into(),
email: "test@example.com".into(),
age: 25,
}
}
}
impl UserBuilder {
fn name(mut self, name: &str) -> Self { self.name = name.into(); self }
fn email(mut self, email: &str) -> Self { self.email = email.into(); self }
fn build(self) -> User {
User::new(self.name, self.email, self.age).unwrap()
}
}
// Usage in tests
#[test]
fn admin_user_can_delete_posts() {
let admin = UserBuilder::default()
.name("Admin")
.email("admin@example.com")
.build();
// ...
}// RAII fixture — cleanup runs automatically
struct TempDatabase {
path: std::path::PathBuf,
conn: DbConnection,
}
impl TempDatabase {
fn new() -> Self {
let path = std::env::temp_dir()
.join(format!("test-db-{}.db", uuid::Uuid::new_v4()));
let conn = DbConnection::create(&path).unwrap();
conn.run_migrations().unwrap();
Self { path, conn }
}
}
impl Drop for TempDatabase {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
#[test]
fn user_created_in_database() {
let db = TempDatabase::new(); // Created
db.conn.insert_user("Alice").unwrap();
assert_eq!(db.conn.count_users().unwrap(), 1);
} // db.drop() called: file deletedMocking with Mockall
use mockall::automock;
#[automock]
trait EmailService: Send + Sync {
fn send(&self, to: &str, subject: &str, body: &str) -> Result<(), EmailError>;
}
#[test]
fn password_reset_sends_email() {
let mut mock_email = MockEmailService::new();
// Set expectation: send() called once with specific args
mock_email
.expect_send()
.with(
mockall::predicate::eq("user@example.com"),
mockall::predicate::str::contains("Reset"),
mockall::predicate::always(),
)
.times(1)
.returning(|_, _, _| Ok(()));
let service = PasswordService::new(Arc::new(mock_email));
service.send_reset_link("user@example.com").unwrap();
// Mock verifies expectations on drop
}Property-Based Testing
use proptest::prelude::*;
// Property: encode then decode is identity
proptest! {
#[test]
fn encode_decode_roundtrip(original in ".*") {
let encoded = base64_encode(&original);
let decoded = base64_decode(&encoded).unwrap();
prop_assert_eq!(original, decoded);
}
#[test]
fn sort_is_idempotent(mut v in prop::collection::vec(any::<i32>(), 0..100)) {
v.sort();
let once_sorted = v.clone();
v.sort();
prop_assert_eq!(v, once_sorted);
}
#[test]
fn add_is_commutative(a in 0i32..1000, b in 0i32..1000) {
prop_assert_eq!(a + b, b + a);
}
}
// Custom strategies
fn valid_email() -> impl Strategy<Value = String> {
r"[a-z]{3,10}@[a-z]{3,10}\.[a-z]{2,4}".prop_map(|s| s)
}
proptest! {
#[test]
fn valid_emails_parse_successfully(email in valid_email()) {
prop_assert!(Email::new(&email).is_ok());
}
}Snapshot Testing with Insta
use insta::assert_snapshot;
#[test]
fn render_user_profile() {
let user = User { name: "Alice".into(), role: Role::Admin, age: 30 };
let rendered = render_profile(&user);
// First run: creates snapshot file
// Subsequent runs: compares against saved snapshot
assert_snapshot!(rendered);
}
// JSON snapshots
use insta::assert_json_snapshot;
#[test]
fn api_response_matches_snapshot() {
let response = api_client.get_user(1).unwrap();
assert_json_snapshot!(response, {
".created_at" => "[timestamp]", // Redact dynamic fields
".id" => "[id]",
});
}# Review and accept new/changed snapshots
cargo install cargo-insta
cargo insta reviewBenchmarks with Criterion
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "my_bench"
harness = false// benches/my_bench.rs
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
fn fibonacci(n: u64) -> u64 {
match n {
0 | 1 => n,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
fn bench_fibonacci(c: &mut Criterion) {
let mut group = c.benchmark_group("fibonacci");
for size in [10u64, 20, 30] {
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
b.iter(|| fibonacci(black_box(size)));
});
}
group.finish();
}
// Compare implementations
fn bench_string_ops(c: &mut Criterion) {
let mut group = c.benchmark_group("string_creation");
let s = "hello world";
group.bench_function("to_string", |b| b.iter(|| black_box(s).to_string()));
group.bench_function("to_owned", |b| b.iter(|| black_box(s).to_owned()));
group.bench_function("String::from", |b| b.iter(|| String::from(black_box(s))));
group.finish();
}
criterion_group!(benches, bench_fibonacci, bench_string_ops);
criterion_main!(benches);cargo bench # Run all benchmarks
cargo bench -- "fibonacci" # Run matching benchmarks
cargo bench -- --save-baseline main # Save baseline
cargo bench -- --baseline main # Compare to baselineDoctest Examples
/// Parses a version string into components.
///
/// # Examples
///
/// ```
/// use mylib::parse_version;
///
/// let (major, minor, patch) = parse_version("1.2.3").unwrap();
/// assert_eq!(major, 1);
/// assert_eq!(minor, 2);
/// assert_eq!(patch, 3);
/// ```
///
/// Returns `None` for invalid version strings:
///
/// ```
/// use mylib::parse_version;
/// assert!(parse_version("not-a-version").is_none());
/// ```
pub fn parse_version(s: &str) -> Option<(u32, u32, u32)> {
// ...
}cargo test --doc # Run only doctestsBest Practices
1. Arrange-Act-Assert (AAA) Structure
Always split your test blocks visually into three distinct phases for maximum readability:
- Arrange: Set up requirements, parameters, and mock states.
- Act: Call the unit or method under test.
- Assert: Validate results against expected outcomes.
#[test]
fn withdraw_reduces_account_balance() {
// Arrange
let mut account = Account::new(dec!(100.00));
let amount = dec!(40.00);
// Act
account.withdraw(amount).unwrap();
// Assert
assert_eq!(account.balance(), dec!(60.00));
}2. RAII Test Fixtures for Setup/Cleanup
Use structs implementing the Drop trait to handle setup and cleanup actions, ensuring database connections, file handles, or mock servers are torn down even if the test panics.
struct TestDbFixture {
pub conn_str: String,
}
impl TestDbFixture {
fn new() -> Self {
let conn_str = format!("test_db_{}", uuid::Uuid::new_v4());
// Initialize test database...
Self { conn_str }
}
}
impl Drop for TestDbFixture {
fn drop(&mut self) {
// Tear down test database...
}
}
#[test]
fn query_returns_inserted_record() {
let fixture = TestDbFixture::new();
// database operations using fixture.conn_str ...
// automatically dropped at scope exit
}Running Tests
cargo test # All tests
cargo test user # Tests with "user" in name
cargo test -- --nocapture # Show println! output
cargo test -- --test-threads=1 # Single-threaded (for shared state tests)
cargo test --lib # Unit tests only
cargo test --test integration_test # Specific integration test file
cargo test --doc # Doctests only
cargo test --release # Tests in release modeTest Coverage
cargo install cargo-tarpaulin # Linux/macOS
cargo tarpaulin --out Html --output-dir coverage/
# Or with llvm-cov (cross-platform)
cargo install cargo-llvm-cov
cargo llvm-cov --htmlTest Suite Checklist
- Unit-test pure logic close to the module under test.
- Put public behavior and cross-module flows in
tests/integration tests. - Use property tests for parsers, serializers, state machines, and invariants.
- Keep mocks at process or trait boundaries, not around simple domain logic.
- Add regression tests before fixing bugs.
- Run doctests for public examples that users may copy.
References
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
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.

