← All notes

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

Sources

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.

License: CC-BY-4.0 · 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).

Cite this

Permanent ID: iil:note:f2807de7

iinventedlight. 2026. "Writing tests in Rust." iinventedlight Library, iil:note:f2807de7, version of 2026-11-09. https://iinventedlight.com/library/notes/rust-writing-tests/. License: CC-BY-4.0.

@misc{iil_note_f2807de7,
  author = {{iinventedlight}},
  title = {{Writing tests in Rust}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-09},
  url = {https://iinventedlight.com/library/notes/rust-writing-tests/},
  note = {Permanent ID iil:note:f2807de7. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-writing-tests.md.