TE

Rust Testing

Rust testing conventions

Details

Language / Topic
rustRust
Category
Testing

Rules

balanced

Rust Testing

- Use `#[cfg(test)]` module in each source file for unit tests. Use `assert_eq!`, `assert_ne!`, `assert!` macros. Put integration tests in `tests/` directory — each file is a separate test binary.

Rust Testing

- Use `#[cfg(test)]` module in each source file for unit tests. Use `assert_eq!`, `assert_ne!`, `assert!` macros. Put integration tests in `tests/` directory — each file is a separate test binary.
- Use `#[should_panic(expected = "message")]` for testing error conditions. Use `Result<(), Box<dyn Error>>` as test return type for `?` operator in tests. Use `cargo test -- --nocapture` to see stdout. Organize test helpers in `tests/common/mod.rs`. Use `#[ignore]` for slow tests, run with `cargo test -- --ignored`.