Integration tests in Rust: tests/ and the public API
Answer: The Rust community groups tests into unit tests and integration tests. Unit tests stay with a module and can use private interfaces. Integration tests live in tests/ and call only the public API. cargo test runs them.
Two categories of tests
Two main categories are used in the Rust community: unit tests, and integration tests.
A unit test is small and focused. It usually covers one module in isolation and may use private interfaces.
Integration tests are entirely external to your library. They use only the public interface.
Annotate a tests module with cfg(test) so that test code compiles only when you run tests.
Where the files go, and how to run them
Place unit tests in the modules they test.
Put integration tests in their own tests/ directory.
Each file under tests/ is its own integration test.
cargo test is an easy way to run all of your tests.
See also
Claims
- id: C1 claim: Rust groups tests into unit tests and integration tests. source: S1 evidence: "unit tests and integration tests"
- id: C2 claim: Integration tests use only the public interface. source: S1 evidence: "using only the public interface"
- id: C3 claim: Integration tests belong in a tests/ directory. source: S2 evidence: "their own tests/ directory"
- id: C4 claim: cargo test runs the tests. source: S2 evidence: "cargo test"
Sources
- [S1] Test Organization. The Rust Programming Language. https://doc.rust-lang.org/book/ch11-03-test-organization.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] Testing with cargo test. Rust By Example. https://doc.rust-lang.org/rust-by-example/cargo/test.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Test Organization; Rust By Example, Testing with cargo test. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Test Organization; Rust By Example, Testing with cargo test. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Integration tests in Rust: tests/ and the public API." iinventedlight Library, iil:note:qc8r4y3f, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-integration-tests/. License: CC-BY-4.0.
@misc{iil_note_qc8r4y3f,
author = {{iinventedlight}},
title = {{Integration tests in Rust: tests/ and the public API}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-integration-tests/},
note = {Permanent ID iil:note:qc8r4y3f. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-integration-tests.schema.json (schema; CC-BY-4.0)
- rust-integration-tests.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-integration-tests.md.