rust-testing
What it does
This code helps you create and organize tests for programs written in the Rust language. It teaches you how to verify if your code works correctly, following a method called TDD (Test-Driven Development).
When to use it
- βWhen writing new parts of your Rust code.
- βWhen adding quality checks to existing code.
- βWhen you want to ensure your code works with different types of input.
Skill link
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/rust-testing/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βHow can I test if my function that adds numbers works correctly in Rust?β
5 new skills a week. 1 minute to read.
How it works
It works by guiding you to write a test that fails first, then write the minimal code to make it pass, and then refactor. This ensures the code is correct and well-written.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Rust Testing Patterns
Comprehensive Rust testing patterns for writing reliable, maintainable tests following TDD methodology.
When to Use
- Writing new Rust functions, methods, or traits
- Adding test coverage to existing code
- Creating benchmarks for performance-critical code
- Implementing property-based tests for input validation
- Following TDD workflow in Rust projects
How It Works
- Identify target code β Find the function, trait, or module to test
- Write a test β Use
#[test]in a#[cfg(test)]module, rstest for parameterized tests, or proptest for property-based tests - Mock dependencies β Use mockall to isolate the unit under test
- Run tests (RED) β Verify the test fails with the expected error
- Implement (GREEN) β Write minimal code to pass
- Refactor β Improve while keeping tests green
- Check coverage β Use cargo-llvm-cov, target 80%+
TDD Workflow for Rust
The RED-GREEN-REFACTOR Cycle
RED β Write a failing test first
GREEN β Write minimal code to pass the test
REFACTOR β Improve code while keeping tests green
REPEAT β Continue with next requirement
Step-by-Step TDD in Rust
// RED: Write test first, use todo!() as placeholder
pub fn add(a: i32, b: i32) -> i32 { todo!() }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() { assert_eq!(add(2, 3), 5); }
}
// cargo test β panics at 'not yet implemented'
// GREEN: Replace todo!() with minimal implementation
pub fn add(a: i32, b: i32) -> i32 { a + b }
// cargo test β PASS, then REFACTOR while keeping tests green
Unit Tests
Module-Level Test Organization
// src/user.rs
pub struct User {
pub name: String,
pub email: String,
}
impl User {
pub fn new(name: impl Into<String>, email: impl Into<String>) -> Result<Self, String> {
let email = email.into();
if !email.contains('@') {
return Err(format!("invalid email: {email}"));
}
Ok(Self { name: name.into(), email })
}
pub fn display_name(&self) -> &str {
&self.name
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn creates_user_with_valid_email() {
let user = User::new("Alice", "alice@example.com").unwrap();
assert_eq!(user.display_name(), "Alice");
assert_eq!(user.email, "alice@example.com");
}
#[test]
fn rejects_invalid_email() {
let result = User::new("Bob", "not-an-email");
assert!(result.is_err());
assert!(result.unwrap_err().contains("invalid email"));
}
}
Assertion Macros
assert_eq!(2 + 2, 4); // Equality
assert_ne!(2 + 2, 5); // Inequality
assert!(vec![1, 2, 3].contains(&2)); // Boolean
assert_eq!(value, 42, "expected 42 but got {value}"); // Custom message
assert!((0.1_f64 + 0.2 - 0.3).abs() < f64::EPSILON); // Float comparison
Error and Panic Testing
Testing Result Returns
#[test]
fn parse_returns_error_for_invalid_input() {
let result = parse_config("}{invalid");
assert!(result.is_err());
// Assert specific error variant
let err = result.unwrap_err();
assert!(matches!(err, ConfigError::ParseError(_)));
}
#[test]
fn parse_succeeds_for_valid_input() -> Result<(), Box<dyn std::error::Error>> {
let config = parse_config(r#"{"port": 8080}"#)?;
assert_eq!(config.port, 8080);
Ok(()) // Test fails if any ? returns Err
}
Testing Panics
#[test]
#[should_panic]
fn panics_on_empty_input() {
process(&[]);
}
#[test]
#[should_panic(expected = "index out of bounds")]
fn panics_with_specific_message() {
let v: Vec<i32> = vec![];
let _ = v[0];
}
Integration Tests
File Structure
my_crate/
βββ src/
β βββ lib.rs
βββ tests/ # Integration tests
β βββ api_test.rs # Each file is a separate test binary
β βββ db_test.rs
β βββ common/ # Shared test utilities
β βββ mod.rs
Writing Integration Tests
// tests/api_test.rs
use my_crate::{App, Config};
#[test]
fn full_request_lifecycle() {
let config = Config::test_default();
let app = App::new(config);
let response = app.handle_request("/health");
assert_eq!(response.status, 200);
assert_eq!(response.body, "OK");
}
Async Tests
With Tokio
#[tokio::test]
β¦
FAQ
What is TDD?
TDD stands for Test-Driven Development. It's a technique where you write a test before writing the actual code. The test fails, you write the code to make it pass, and then you improve the code.
What are unit tests?
Unit tests check small, isolated parts of your code, like a single function or method, to ensure they work as expected.
What are integration tests?
Integration tests check how different parts of your code work together. They test the system as a whole or large components.
Details
Original description by the author
Rust testing patterns including unit tests, integration tests, async testing, property-based testing, mocking, and coverage. Follows TDD methodology. Use when writing Rust tests β unit, integration, async, property-based, or coverage.
repo: affaan-m/ECC
Popularity
- GitHub stars
- 280k
- Ranking position
- #243
- License
- MIT
- Updated
- 10/15/2026
Comments
- β¦
More skills from this repository
accessibility
280kHelps your agent create interfaces that everyone can use. Ensures websites and apps work well for people with disabilities, like those who are blind or have reduced mobility.
agent-architecture-audit
280kMakes your agent check if everything is working correctly. It finds and fixes problems in AI systems, ensuring they perform as expected.
agent-eval
280kHelps your agent compare different AI coding tools. It measures which one is faster, cheaper, and more reliable for specific tasks.
agent-harness-construction
280kMakes your agent better understand how to perform tasks. It optimizes the tools and instructions the agent uses to increase its chances of success.
Similar skills
test-driven-development
311kTeaches your agent to write a failing test first, watch it fail, then write the smallest code that passes, before building any feature or fix.
verification-before-completion
311kTeaches your agent to run real checks and read their output before claiming that a task, fix, or build is complete or passing.
migrate-to-shoehorn
310kReplaces risky type casts in your test files with the shoehorn library, so tests can use partial data and still pass type checks.