---
source: https://iinventedlight.com/library/notes/rust-writing-tests.md
title: "Writing tests in Rust"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (rust-lang/book and cited companions); prose restates cited source lines in own words"
license_family: rust-mit
attribution: "Summary by iinventedlight (AI-assisted). Sources: Rust book + Rust by Example Unit testing (MIT OR Apache-2.0). Notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Rust by Example (rust-lang/rust-by-example): Copyright (c) 2014 Jorge Aparicio; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-09
status: published
approved_by: "Cole Withington, 2026-11-09 13:40 PT: \"Yes, publish all 15 Rust drafts\""
checked_on: 2026-11-09
as_of: 2026-11-09
confidence: medium
untested: "No Rust code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-systems: Rust testing"
tags: [rust, testing, cargo, assert]
client: Librarian
sources:
  - id: S1
    title: "How to Write Tests"
    publisher: The Rust Project (The Rust Programming Language book)
    url: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
    accessed: 2026-11-09
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "Test Organization"
    publisher: The Rust Project (The Rust Programming Language book)
    url: https://doc.rust-lang.org/book/ch11-03-test-organization.html
    accessed: 2026-11-09
    license: "MIT OR Apache-2.0"
  - id: S3
    title: "Unit testing - Rust By Example"
    publisher: Rust by Example (rust-lang/rust-by-example)
    url: https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html
    accessed: 2026-11-09
    license: "MIT OR Apache-2.0"
---
# Writing tests in Rust

**Answer:** Add `#[test]` on the line before `fn` to create a test function. `cargo test` builds a runner that executes those annotated functions.

## Assert macros

`assert!` from the standard library checks that a test condition evaluates to true.

## Organization

Unit tests isolate each unit of code from the rest. Integration tests are external to your library and use your code like external code would.

Rust by Example marks test functions with `#[test]`; failing tests panic.

## Claims

- id: C1
  claim: Add #[test] on the line before fn to mark a test.
  source: S1
  evidence: "add #[test] on the line before fn."
- id: C2
  claim: The assert! macro ensures a condition in a test holds.
  source: S1
  evidence: "The assert! macro, provided by the standard library, is useful when you want to ensure"
- id: C3
  claim: Unit tests test each unit of code in isolation.
  source: S2
  evidence: "The purpose of unit tests is to test each unit of code in isolation from"
- id: C4
  claim: Integration tests are entirely external to your library.
  source: S2
  evidence: "Integration tests are entirely external to your library and use your code in the same"

- id: C5
  claim: Test functions are marked with the #[test] attribute.
  source: S3
  evidence: "Test functions are marked with the #[test] attribute."

## Sources

- [S1] How to Write Tests. The Rust Project (The Rust Programming Language book). https://doc.rust-lang.org/book/ch11-01-writing-tests.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.
- [S2] Test Organization. The Rust Project (The Rust Programming Language book). https://doc.rust-lang.org/book/ch11-03-test-organization.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.

- [S3] Unit testing - Rust By Example. Rust by Example (rust-lang/rust-by-example). https://doc.rust-lang.org/rust-by-example/testing/unit_testing.html . License: MIT OR Apache-2.0. Accessed 2026-11-09.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: The Rust Programming Language book (rust-lang/book): Copyright (c) 2010 The Rust Project Developers; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Rust by Example (rust-lang/rust-by-example): Copyright (c) 2014 Jorge Aparicio; MIT OR Apache-2.0, used under MIT (https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
