
Test Organization in Rust for Large Workspaces Explained
Organize Rust tests in a large workspace: unit tests beside code, integration tests per crate, a shared test-support crate, slow test gating and what to run in CI.
Unit, doc and integration tests, property testing and mocks, enough to refactor without flinching.

Organize Rust tests in a large workspace: unit tests beside code, integration tests per crate, a shared test-support crate, slow test gating and what to run in CI.

Test code that calls out to services with a hand-written fake or a Mockall mock. Compare setup cost, what each can assert and the cases where a fake is the better tool.

Put logic in a library crate and keep main.rs thin, so tests can call functions, integration tests see the public API and other binaries can reuse the code.

Test an Axum router without opening a port: build a Request, call oneshot, read the body and assert on status and JSON. Includes the setup for shared state.

Instead of picking inputs, state a property and let proptest generate hundreds. See round trip and invariant properties, shrinking to a minimal failure and a bug it finds.

Test an Axum API against a real Postgres started by Testcontainers: one container per test binary, one migrated database per test, the leaks and version traps that bite, and measured timings for each isolation strategy.

assert, assert_eq, assert_ne, matches and debug_assert, with custom failure messages, float comparison, and what each prints. Includes a helper for readable domain assertions.

Code blocks in doc comments compile and run as tests. Write examples that stay correct, hide setup lines with a hash, mark intentional failures and choose the right attributes.

Feature flags multiply build configurations. See the combinations that fail, how cargo hack tests them, and rules for additive features that keep builds safe.

Files in the tests directory are separate crates that use your library like any outside user. Learn the layout, shared helpers without duplicate test binaries and testing a binary crate.

Put unit tests in a cfg(test) module at the bottom of the file they test. See how the test harness discovers them, the access to private items and naming conventions that read well.

A GitHub Actions workflow for Rust that formats, lints, tests and builds, with a pinned toolchain, dependency caching, a matrix and the failure modes to avoid.
We send one email when a new post is published. No spam.