Rust TDD
Testing & Qualityrust-tddRust TDD is an Agent Skill for testing & quality published in the majiayu000/claude-skill-registry repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
Test-Driven Development for Rust. Use when creating new functionality, writing tests, or fixing bugs through the Red-Green-Refactor TDD cycle.
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Inspect the Agent Skill at https://github.com/majiayu000/claude-skill-registry/blob/HEAD/skills/development/rust-tdd/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/rust-tdd, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
Rust TDD Workflow
Process (Red-Green-Refactor)
1. Red Phase
Write a failing test first that describes the desired behavior:
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_feature() {
let result = new_function(input);
assert_eq!(result, expected_output);
}
}
Verify the test fails: cargo test -p <crate> <test_name>
2. Green Phase
Implement the minimum code to make the test pass:
- Write only enough code to pass the test
- Don't over-engineer or add extra features
- Focus on correctness, not elegance
Verify: cargo test -p <crate>
3. Refactor Phase
Improve the code while keeping tests green:
- Remove duplication
- Improve naming
- Simplify logic
- Extract functions if needed
After each change: cargo test -p <crate>
Test Commands
# Run specific test
cargo test -p <crate> <test_name> -- --nocapture
# Run all tests in crate
cargo test -p <crate> --all-features
# Run with output
cargo test -p <crate> -- --nocapture
# Run doc tests
cargo test -p <crate> --doc
# Continuous testing
cargo watch -x "test -p <crate>"
Test Patterns
Basic Test
#[test]
fn test_success_case() {
let result = function_under_test(42);
assert_eq!(result, 84);
}
Result-returning Test
#[test]
fn test_with_result() -> Result<(), Box<dyn std::error::Error>> {
let result = fallible_function()?;
assert_eq!(result, expected);
Ok(())
}
Panic Test
#[test]
#[should_panic(expected = "index out of bounds")]
fn test_panic_case() {
function_that_panics();
}
Async Test
#[tokio::test]
async fn test_async_function() {
let result = async_function().await;
assert!(result.is_ok());
}
Property-based Test (with proptest)
use proptest::prelude::*;
proptest! {
#[test]
fn test_property(x in 0..100i32) {
let result = function(x);
prop_assert!(result >= 0);
}
}
Final Verification
After completing TDD cycle:
cargo clippy -p <crate> -- -D warnings
cargo fmt -p <crate> -- --check
cargo test -p <crate> --all-features
Nebula-specific Patterns
- Each crate has its own error type via
thiserror - Use
#[tokio::test]for async tests - Place unit tests in same file under
#[cfg(test)]module - Place integration tests in
tests/directory of the crate