Testing
A test in Rust is an ordinary function with the #[test] attribute: it passes if it returns and fails if it panics. No separate framework is needed — the test harness ships with the standard toolchain, and cargo test builds the tests into a separate program, runs them and prints a report. Checks inside a test are written with the assert!, assert_eq! and assert_ne! macros: on failure they panic and show the values being compared.
Tests come in three kinds, and cargo test runs them one after another. Unit tests live next to the code in a #[cfg(test)] mod tests module and see private functions. Integration tests live in the tests/ folder: each file there is a separate crate that has access only to the public API. Doc tests are code examples in /// documentation comments that cargo test compiles and runs, so the documentation does not fall behind the code. Attributes cover the special cases: #[should_panic] expects a panic, #[ignore] sets a slow test aside, and a test that returns Result can use the ? operator.
Two properties explain most surprises: tests run in parallel, each on its own thread, and their output is captured and shown only for the ones that fail. This topic walks testing layer by layer — from the first #[test] to the examples in the documentation.
Topic map
- Unit Tests —
#[cfg(test)] mod testsanduse super::*; a test sees private functions;cargo testand filtering by name; parallel threads and--test-threads=1; captured output,--show-outputand--no-capture. - Assert Macros —
assert!,assert_eq!,assert_ne!and custom messages; theleft/rightfailure format; why the type needsPartialEqandDebug; floats,matches!andassert_matches!; property tests withproptest. - should_panic, Result and ignore —
#[should_panic(expected = "…")]checks a substring of the message; a test that returnsResult, and?inside it;#[ignore],--ignoredand--include-ignored. - Integration Tests — the
tests/folder, each file a separate crate with access only to the public API; shared code intests/common/mod.rs; why the logic moves intolib.rs; swapping dependencies through a trait. - Doc Tests — examples in
///are compiled and run for libraries; hidden#lines; theshould_panic,no_run,compile_failandignoreattributes; whycargo test nameskips them.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Forgetting use super::* in the tests module | error[E0425]: cannot find function `greeting` in this scope: a child module does not see its parent's names on its own |
Comparing a type without PartialEq or Debug in assert_eq! | Errors E0369 and E0277: the macro has to compare the values and print them on failure |
Writing #[should_panic] without expected | The test passes on any panic, even an out-of-bounds index on a vector while setting up data |
| Sharing a file, an environment variable or the current directory between tests | Tests run in parallel and get in each other's way: the result changes from run to run |
Looking for the println! output of a passing test | The output is captured; --show-output or --no-capture shows it |
Calling a private function from tests/ or from a doc test | error[E0603]: function `normalize` is private: both use the library from outside |
Putting shared code in tests/common.rs | Cargo treats the file as a separate test crate and prints an empty run, running 0 tests, for it; the right place is tests/common/mod.rs |
Why it matters in interviews
In a junior Rust interview testing is checked as a habit rather than as theory: you are asked to write a test for a function, explain where it lives and why it sees private code, or read the output of a failed cargo test. Questions a level up move to strategy: how to test code that talks to the network or a database, and what to add to example-based tests.
What interviewers usually check:
- Where unit tests live, why
#[cfg(test)]is needed and why a test sees private functions. - Why
assert_eq!is more convenient thanassert!and what it prints on failure. - When you need
#[should_panic]withexpected, a test that returnsResult, and#[ignore]. - How integration tests in
tests/differ from unit tests and why they can reach only the public API. - What doc tests are and why only libraries have them.
- How to test code with external dependencies: swapping them through a trait, property tests.