Integration tests
Integration tests live in tests/ and compile as separate crates that depend on your library like any other client. They can see only what is pub.
Detailed Explanation
tests/parse.rs is a crate. It writes use mylib::parse; and calls the public API. It cannot reach mylib::parse_inner unless you published it. That is the contract: an integration test is a client. If a test needs private items, it is a unit test and belongs in src/ under #[cfg(test)].
Each file in tests/ is its own crate by default, so they do not share helpers unless you add a tests/common/mod.rs module (not a crate) or a tests/common.rs. A tests/common.rs that is not a test file should not contain #[test] if you only wanted helpers; name it so Cargo treats it as a module, or use the mod common; pattern from the book.
Integration tests link your crate as a library. A binary-only crate (main.rs and no lib.rs) has no library for tests/ to depend on. Split a lib.rs and have main.rs call it; then integration tests can use the lib. That split is good design even without tests.
cargo test --test parse runs one file. cargo test runs unit tests, integration tests, and doc tests. Integration tests are slower to compile because each file is a crate. Do not put a thousand tiny files in tests/ if a module of unit tests would do.
When you write an integration test: the public surface, a real file, a feature combination. When you write a unit test: private logic. When you write an end-to-end test against a running server: still tests/, or a separate crate, plus more setup than a unit test.
Code Examples
In a library crate this would be tests/add.rs:
// In a library crate this would be tests/add.rs:
// use mylib::add;
// #[test]
// fn adds() { assert_eq!(add(2, 40), 42); }
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("{}", add(2, 40)); // prints: 42
}Common Gotchas
Integration tests cannot see private items
That is the feature. Make a unit test, or publish a `pub(crate)` API is not enough — they are another crate. You need `pub`.
A binary crate has nothing for `tests/` to link
Add `src/lib.rs` and keep `main` thin.
`cargo test --test name` selects one integration crate
The name is the file stem.