Real Projects · 16 min · free lesson

Testing

Tests turn behaviour into executable evidence.

Core idea

#[cfg(test)] selects test builds, including cargo test --release; ordinary builds exclude that module.

Diagram for Testing: a Rust code example flows to the lesson rule and the key observation.
Tests live beside the code and can see its private parts.

Understand it

#[test] registers functions with the test harness. A #[cfg(test)] module is excluded from ordinary builds, including cargo build --release, but included by cargo test --release. Test mode and optimization profile are separate choices. Unit tests sit beside the code and can reach its private details; integration tests live in tests/ and see only the public API, which is a useful check on your own interface.

A test records an observable promise. #[test] marks a function that the test harness runs under cargo test; an assertion compares the actual value with a prediction. The sample puts tests beside the implementation in a #[cfg(test)] module, which is a good place for small unit tests of edge cases and private helpers. The module is only compiled when testing, so it does not become a normal production code path.

Start with a function that adds two numbers. Test a typical case, zero, and a boundary that might reveal overflow or an incorrect sign. Name tests for the behavior they protect rather than the implementation line they execute. If a test fails, its assertion should tell you what input produced the mismatch. A test that merely repeats the function's code is weak: it can share the same mistake. Prefer a simple expected result you can justify independently.

Integration tests under tests/ compile as separate crates and call the public library API. They are useful when you want evidence that an external consumer can use the package as intended. A practical suite usually combines focused unit tests with a few end-to-end cases. Tests do not prove an implementation correct for every input, but they make intended behavior executable and guard against later changes. Run the narrowest relevant test while editing and the full suite before shipping; both speeds of feedback matter.

Compare cargo test and cargo test --release: both execute assertions. Integer overflow checks normally differ between the default dev and release profiles, and projects can override that policy. Write explicit checked arithmetic when overflow is part of the API contract, rather than relying on the test profile.

Read the code

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn adds_two_numbers() {
        assert_eq!(add(2, 2), 4);
    }
}

Tests live beside the code and can see its private parts.

Practice starter

fn compute() -> i32 { 2 + 2 }

#[test]
fn works() {
    let result = compute();
}

Practise this code in Playground ↗

Try it

Add one success case and one boundary case for a pure function.

What success looks like: Both are deterministic and fail for a deliberately broken implementation.

Check your understanding

What is missing from that test?

Reveal the answer
  1. A return type so the harness can read the result
  2. An assertion — the test passes no matter what compute returns
  3. The #[cfg(test)] attribute above the module

A test with no assertion only proves the code did not panic.

Go deeper

Common misconception: “A test with no assertion still checks that the result is right.”

This test can fail only by panicking, so without an assertion it passes whatever compute returns. Add assert_eq! with a value you can justify.

  1. Find the module: #[cfg(test)] compiles mod tests only for the test harness, so a normal build never sees it.
  2. Import the code: use super::* brings in the parent module's items, including private ones, which is how add is in scope.
  3. Run the assertion: #[test] registers adds_two_numbers. assert_eq!(add(2, 2), 4) panics on a mismatch, and the harness reports that panic as a failure.

Transfer challenge: Add one test for add(0, 5) and one for add(i32::MAX, 1). Predict which one panics under cargo test, then compare with cargo test --release.

Connections

  • Modules: Cargo.toml and sibling files
  • Error Design: Propagation and custom errors
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: Testing (courseId "projects", lessonId "testing")
Link: https://crabrix.com/learn/projects/testing/
Goal: #[cfg(test)] selects test builds, including cargo test --release; ordinary builds exclude that module.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=projects/testing) and I will write the code there.
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.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.