Testing
52 articles
Best Practices for Organizing Tests in Rust Projects
Organize Rust tests by placing unit tests in the same file as the code they test using `#[cfg(test)]` modules, and integration tests in a separate `tests` directory.
How to Add Examples to Rust Documentation
Add Rust code examples to documentation by creating listing directories and using the rustdoc_include directive in markdown files.
How to Benchmark Code in Rust
Use the Criterion crate with `cargo bench` to measure and compare the execution speed of your Rust functions.
How to benchmark with criterion
Add criterion to dev-dependencies, create a harness=false bench file, and run cargo bench with the walltime feature.
How to Document Public APIs in Rust: Best Practices
Document public Rust APIs by adding `///` doc comments with examples and running `cargo doc` to generate the reference.
How to Generate Documentation with cargo doc
Run `cargo doc` in your project root to generate HTML documentation for your crate and its dependencies, then open the output in `target/doc/index.html`.
How to handle errors in tests
Handle test errors by using unwrap() or expect() to panic immediately on failure.
How to Ignore Tests and Run Them Conditionally
Skip tests with #[ignore] and run them later using cargo test -- --ignored or --include-ignored flags.
How to Link to Other Items in Rust Docs with Intra-Doc Links
Link to other items in Rust docs using relative markdown paths or Rust item paths with the standard link syntax.
How to measure code coverage
Use the external cargo-tarpaulin tool to generate an HTML report showing which lines of Rust code are executed by your tests.
How to Measure Code Coverage in Rust
Install cargo-tarpaulin and run cargo tarpaulin to generate an HTML report of your Rust code coverage.
How to mock dependencies
Implement a MockMessenger struct using RefCell to record messages for testing LimitTracker without external dependencies.
How to Mock Dependencies in Rust (mockall, mockito)
Use the mockall crate to generate mock implementations of traits for isolated unit testing in Rust.
How to Run a Specific Test in Rust with Cargo
You can run a specific test by passing its name as an argument to `cargo test`, using either an exact match or a substring filter.
How to Run Tests in Parallel vs Sequentially in Rust
Set RUST_TEST_THREADS=1 to run Rust tests sequentially or omit it to run them in parallel.
How to test async code
Run async Rust tests by adding the #[tokio::test] attribute to your async test functions to provide the necessary runtime environment.
How to test async code in Rust
Use the `tokio` runtime with the `#[tokio::test]` attribute to run async tests, as standard `#[test]` attributes cannot handle `async` functions directly.
How to Test Command-Line Applications in Rust (assert_cmd)
Test Rust CLI apps by spawning the binary with assert_cmd and asserting exit codes and output.
How to test database code in Rust
Test Rust database code by adding #[test] functions and running cargo test.
How to Test Embedded Rust Code
Run cargo test for standard projects or mdbook test with library paths for book-based embedded Rust code.
How to Test Error Cases in Rust
Verify error cases in Rust by marking test functions with #[should_panic] to assert that specific inputs cause a crash.
How to test error conditions
Use the #[should_panic] attribute on a test function to verify that error conditions cause the expected crash.
How to Test for Expected Panics with #[should_panic]
Use the #[should_panic] attribute with an expected message string to verify that a function panics correctly.
How to test private functions
Test private Rust functions by defining tests within the same module using the #[cfg(test)] attribute to access internal logic directly.
How to Test Private Functions in Rust
Test private Rust functions by adding a #[cfg(test)] module and using use super::* to access internal items.
How to Use assert!, assert_eq!, and assert_ne! in Rust
Use `assert!` to verify a condition is true, `assert_eq!` to check two values are equal, and `assert_ne!` to ensure they are different; all three panic if the check fails. Place these macros inside test functions annotated with `#[test]` or directly in `main` to validate logic immediately.
How to Use cargo-nextest for Faster Test Runs
Install cargo-nextest and run cargo nextest run to execute Rust tests in parallel for faster feedback.
How to Use cargo-tarpaulin for Code Coverage
Run cargo tarpaulin to measure test coverage and generate an HTML report for your Rust project.
How to use cargo test features
Run cargo test with the --features flag to execute tests for optional code blocks enabled by specific feature names.
How to Use #[doc(hidden)] to Hide Items from Documentation
Use the #[doc(hidden)] attribute above an item to hide it from generated Rust documentation while keeping it accessible in code.
How to use doc tests
Run mdbook test with the correct library path to execute doc tests in your Rust book.
How to Use Markdown in Rust Documentation
Write Markdown files in the src directory and run mdbook build to generate HTML documentation.
How to use proptest for property testing
Use proptest to define property-based tests that automatically generate random inputs to verify your code's logic holds true across all scenarios.
How to use rstest
Install rstest via Cargo and use the #[rstest] macro to create parameterized tests with fixtures.
How to Use Snapshot Testing in Rust (insta crate)
Use the insta crate to add snapshot tests by wrapping assertions in assert_snapshot! and reviewing changes with cargo insta.
How to use test-case crate for parameterized tests
Use the test-case crate to run a single test function with multiple argument sets via the #[test_case] macro.
How to use test fixtures
Use string-based test fixtures with `//-` directives and `$0` cursors to simulate multi-file Rust projects for IDE and compiler testing.
How to Use Test Fixtures and Setup/Teardown in Rust
Mark test functions with #[test], use #[cfg(test)] for modules, and call shared setup functions from a common module to manage fixtures.
How to Use Test Fixtures with the rstest Crate
Add the rstest crate and use the #[rstest] macro with #[case] arguments to run parameterized tests efficiently.
How to Use Test Helpers and Shared Test Utilities
Use #[test] attributes and shared modules to define reusable helper functions for your Rust test suite.
How to Use Test Modules and the #[cfg(test)] Attribute
Wrap test functions in a `#[cfg(test)]` module to run them exclusively with `cargo test`.
How to Write Async Tests in Rust
Write async tests in Rust by adding the tokio dependency and using the #[tokio::test] attribute on your async functions.
How to Write Async Tests in Rust (with tokio::test)
Write async tests in Rust by adding the tokio dependency and using the #[tokio::test] attribute on your async functions.
How to Write Doc Tests in Rust
Write doc tests by adding runnable code examples in triple-backtick blocks within Rust documentation comments.
How to Write Doc Tests That Also Serve as Examples
Write doc tests using /// comments with executable examples and run mdbook test to verify they work.
How to Write Documentation Comments in Rust (/// and //!)
Use /// for item documentation and //! for crate-level documentation to generate API docs with rustdoc.
How to write integration tests
Create a tests directory at your project root and write functions annotated with #[test] that import and verify your library's public API.
How to Write Integration Tests for Rust Web APIs
Create a tests directory at the crate root and write #[test] functions that import your library to verify end-to-end behavior.
How to Write Integration Tests in Rust
Write Rust integration tests by creating files in the `tests` directory and running them with `cargo test`.
How to Write Property-Based Tests in Rust (proptest, quickcheck)
Use the proptest crate to define tests that automatically generate random inputs to verify properties hold true for all cases.
How to write unit tests
Write unit tests by defining a `tests` module within your source file (or in a separate `tests/` directory for integration tests) and using the `#[test]` attribute on functions that assert expected behavior.
How to Write Unit Tests in Rust
Write unit tests by placing `#[test]` annotated functions inside a `tests` module within your source file, then run them with `cargo test`.