Rust Testing
A collection of test-writing best practices for Rust, adapted from the leonardomso/rust-skills catalog (MIT licensed — see NOTICE.md). Designed for AI agents and LLMs to write readable, maintainable, and trustworthy tests.
Note: the paths: glob above matches files under a tests/ directory but cannot match inline #[cfg(test)] modules embedded in ordinary .rs source files — this skill still applies to those, just without path-based auto-triggering.
Categories
Testing [MEDIUM]
Write trustworthy Rust tests with idiomatic structure, mocking, and property-based coverage.
| Rule | Description |
|---|---|
| test-arrange-act-assert | Structure tests with arrange, act, assert |
| test-cfg-test-module | Keep unit tests in a #[cfg(test)] module |
| test-criterion-bench | Benchmark with Criterion, not manual timing |
| test-descriptive-names | Name tests by the behavior they verify |
| test-doctest-examples | Keep doc examples as executable doctests |
| test-fixture-raii | Clean up test fixtures with RAII, not manual teardown |
| test-integration-dir | Put integration tests in the tests/ directory |
| test-loom-concurrency | Model-Check concurrent code with Loom |
| test-mock-traits | Depend on traits, not concrete types, to enable mocking |
| test-mockall-mocking | Use Mockall to generate trait mocks |
| test-proptest-properties | Test invariants with property-based testing (proptest) |
| test-should-panic | Assert expected panics with #[should_panic] |
| test-snapshot-testing | Snapshot-Test large or structured output with Insta |
| test-tokio-async | Drive async tests with #[tokio::test] |
| test-use-super | Import parent items in tests with use super::* |
Quick Reference
Testing
#[test]
fn new_user_has_correct_name() {
// Arrange
let name = "alice";
let email = "alice@example.com";
// Act
let user = User::new(name, email).unwrap();
// Assert
assert_eq!(user.name(), "alice");
}
See Also
- rust-coding-standards - General Rust coding standards and best practices
- rust-tooling - clippy, rustfmt, and Cargo workspace/project-structure rules
- NOTICE - MIT attribution for content adapted from leonardomso/rust-skills