
Understand it
Files under tests/ compile as separate crates and can only use the library's public API. That boundary catches accidental reliance on private implementation details.
An integration test behaves like an external user of your library. Each file under tests/ is compiled as a separate crate and imports the public API by crate name. It cannot reach private helpers simply because they exist in the same repository. That constraint is valuable: if a test can accomplish a real task through the public interface, downstream callers probably can too.
The sample test imports demo::answer and asserts a stable result. Add a second test for a boundary case and a third for an error path. Give each a name that describes the promised behavior. Avoid testing private implementation details through elaborate scaffolding; those tests can become brittle when you refactor without changing the external contract. Unit tests next to the code are still useful for focused private logic. The two test locations serve different levels of confidence.
If an integration test needs a binary or fixture, make setup explicit and keep test cases independent so order does not matter. A test that relies on shared mutable files or network state can fail intermittently and hide real regressions. Run cargo test --test public_api while developing that file, then cargo test for the full suite. When a test fails, distinguish a library behavior change from a test assumption that was never part of the public contract. A small, representative collection of black-box tests often gives more confidence than many tests coupled to internal functions.
Read the code
// tests/public_api.rs
use demo::answer;
#[test]
fn answer_is_stable() {
assert_eq!(answer(), 42);
}The test imports demo::answer by crate name, as any outside caller would.
Practice starter
// src/lib.rs
pub fn answer() -> u32 { helper() }
fn helper() -> u32 { 42 }
// tests/public_api.rs
use demo::helper;
#[test]
fn helper_is_reachable() {
assert_eq!(helper(), 42);
}Try it
Predict the compiler error for this test. Then rewrite it so it checks the same behaviour through answer, leaving helper private.
What success looks like: The test passes through the public API, and you can say what it no longer depends on.
Check your understanding
Why can this test not use private items?
Reveal the answer
- It is a separate crate
- Tests ban privacy
- assert_eq! hides them
A file under tests/ is compiled as its own crate, so it sees only pub items, just as a downstream user would.
Go deeper
Common misconception: “A file under tests/ is part of the library, so it can reach private helpers.”
Files under tests/ compile as separate crates and can only use the library's public API. Reaching private logic is the job of a unit test beside the code.
- Name the crate:
use demo::answer;imports through the library's crate name, becausetests/public_api.rsis a separate crate. - Reach the test:
#[test]registersanswer_is_stable, andcargo test --test public_apiruns only this file. - Compare the result:
assert_eq!(answer(), 42)holds the public promise, so an internal rename that keeps that promise does not touch this test.
Transfer challenge: Add tests/edge_cases.rs beside this file. Predict whether it can call functions defined in public_api.rs, then add a pub fn to the library only if the new test truly needs one.
Connections
- Lockfile Reproducibility: Build the same resolved graph again
- Error Context: Preserve a cause while explaining the failed operation
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Integration Tests (courseId "projects", lessonId "integration-tests") Link: https://crabrix.com/learn/projects/integration-tests/ Goal: Files under tests/ compile as separate crates and can only use the library's public API. Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.