quality-unit-testing
Quality Unit Testing for Rust
Write tests that catch real bugs and provide deployment confidence.
Core Principles
Quality over coverage: Tests should catch real bugs, not just boost percentages.
Quick Reference
Naming: test_<function>_<scenario>_<expected>
#[test]
fn test_process_payment_insufficient_funds_returns_error()
#[tokio::test]
async fn test_withdraw_valid_amount_decreases_balance()
AAA Pattern (Arrange-Act-Assert)
#[test]
fn test_account_withdraw_decreases_balance() {
// Arrange
let mut account = Account::new(100);
// Act
let result = account.withdraw(30);
// Assert
assert!(result.is_ok());
assert_eq!(account.balance(), 70);
}
Isolation
✓ Mock: APIs, databases, file systems, external services ✗ Don't mock: Value types, pure functions, code under test
Single Responsibility
Each test verifies ONE behavior with ONE reason to fail.
Rust-Specific Patterns
// Async tests
#[tokio::test]
async fn test_async_operation() { /* ... */ }
// Result-based tests
#[test]
fn test_operation() -> anyhow::Result<()> { /* ... */ }
// Test builders
let episode = TestEpisodeBuilder::new()
.with_task("Test task")
.completed(true)
.build();
// RAII cleanup
struct TestDb(TempDir);
impl Drop for TestDb { fn drop(&mut self) { /* auto cleanup */ } }
Success Metrics
✓ Deploy without manual testing ✓ Test failures pinpoint exact problems ✓ Refactoring doesn't break unrelated tests ✓ Tests run in milliseconds
Workflow
Creating Tests:
- Understand the code behavior
- Identify risks
- Write failing test first (red-green-refactor)
- Apply AAA pattern
- Isolate dependencies
- Verify speed (milliseconds)
Reviewing Tests:
- Run analysis script
- Check naming conventions
- Ensure isolation
- Confirm single responsibility
More from d-o-hub/rust-self-learning-memory
loop-agent
Execute workflow agents iteratively for refinement and progressive improvement until quality criteria are met. Use when tasks require repetitive refinement, multi-iteration improvements, progressive optimization, or feedback loops until convergence.
51web-search-researcher
Research topics using web search and content fetching to find accurate, current information. Use when you need modern information, official documentation, best practices, technical solutions, or comparisons beyond your training data.
46perplexity-researcher-reasoning-pro
Highest level of research and reasoning capabilities for complex decision-making with significant consequences, strategic planning, technical architecture decisions, multi-stakeholder problems, or high-complexity troubleshooting requiring expert-level judgment and sophisticated reasoning chains. Prioritizes actively maintained repositories and validates website sources for 2025 relevance.
44context-retrieval
Retrieve relevant episodic context from memory for informed decision-making. Use when you need past episodes, patterns, or solutions to similar tasks.
44rust-code-quality
Perform comprehensive Rust code quality reviews against best practices for async Rust, error handling, testing, and project structure
43codebase-analyzer
Analyze implementation details, trace data flow, explain technical workings, locate files, and consolidate codebases. Use when you need to understand HOW code works, find file locations, or assess technical debt.
40