Testing in Rust

Contents
#[test] functions
assert! macros
cargo test
Tests beside code

#[test] functions

A test is a plain function with the test attribute above it. The harness runs each marked function and reports pass or fail per name.

Keep tests small and focused: one behavior per function with a name that states the expected outcome.

fn add(a: i32, b: i32) -> i32 { a + b } #[test] fn adds_two() { assert!(add(1, 1) == 2); }

The add function holds the logic and adds_two checks one case. The attribute is the only marker the harness needs.

assert! macros

Assert macros state what must hold and stop the test with details on mismatch. Use assert for booleans and the equal variants when two values must match.

Clear expected values in the call make failures readable without opening extra logs.

#[test] fn checks() { assert!(1 + 1 == 2); assert_eq!(2 + 2, 4); }

The first line checks a condition and the second compares two values. Assert_eq prints both sides when they differ, which speeds up diagnosis.

cargo test

Run cargo test in the project root. Passing tests print ok lines and a summary count at the end.

Filter by name to run one test during debugging, and run the full suite before every commit.

cargo test test adds_two ... ok

One line per test shows its name and result. A green summary confirms the whole suite stayed healthy.

Tests beside code

Unit tests live in a tests module at the bottom of the same file. The cfg test gate compiles the module for test runs only, so release builds skip it fully.

Import the parent items with a use super path and cover each public function near its definition.

fn add(a: i32, b: i32) -> i32 { a + b } #[cfg(test)] mod tests { use super::add; #[test] fn adds_two() { assert_eq!(add(1, 1), 2); } }

Logic and checks share one file but build separately. New readers find the contract of add right below its body.

Next: Error handling

Article author: Arthur Isaev

Related articles
Rust
Rust course: zero to hero in 50 lessons
What is Rust and where it runs
Install Rust
cargo new
Your first program
Variables
Numbers
Strings
Booleans
Tuples
Arrays
Vectors
Slices
if and else
match
Loops
Ownership
Borrowing
Lifetimes
Functions
Structs
Enums in Rust
Advanced match in Rust
Option combinators in Rust
Result and the question mark in Rust
Advanced strings in Rust
Vec methods in Rust
HashMaps in Rust
Modules in Rust
Crates and workspaces in Rust
Testing in Rust
Error handling
Traits
Generics
Trait bounds
Closures
Iterators
Smart pointers
Threads
Mutex and Arc
Files
CLI arguments
Environment variables
Diagnostics
Time
Parsing strings
TCP networking
Capstone: wordfreq CLI
Capstone: tiny HTTP server
Modern Rust tour
Hero roadmap: the whole course on one page
Install Rust on Windows 11
Install Rust on Ubuntu
Install Rust on Rocky Linux
Install Rust on macOS
Install Rust on FreeBSD
Rust in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: