Rust Testing from Scratch: How to Keep Your Sanity and Write Your First Test

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Testing in Rust Is Not Scary, But Interesting?

\n\n

When you're just starting to learn Rust, it seems like the compiler already does all the dirty work for you. It checks memory, tracks borrows, and prevents you from shooting yourself in the foot. But even the strictest compiler cannot check the program logic. This is where testing comes to the rescue.

\n\n

Many beginners are afraid of tests. They think it's a waste of time, that you need to write a bunch of boring code. But in reality, tests are your safety net. They help you find bugs faster, refactor code confidently, and sleep peacefully after a release.

\n\n

In this article, we'll break down where to start testing in Rust if you're a beginner. No fluff — only practice, real examples, and tips that will be useful in real projects.

\n\n

What You Need to Know Before Starting?

\n\n

Before writing tests, make sure you have Rust installed. If not, open a terminal and run:

\n\n
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
\n\n

Everything we need is already built into the ecosystem. No additional frameworks need to be installed. Rust offers a powerful built-in test runner that can be launched with a single command.

\n\n

Let's create a new project for experiments:

\n\n
cargo new rust_test_guide\ncd rust_test_guide
\n\n

Great! Now we have a clean project where we will learn to write tests.

\n\n

First Test: Understanding with an Example

\n\n

Open the file src/lib.rs. By default it is empty, but we'll add a simple function and a test for it.

\n\n

Let's write a function that checks whether a number is even:

\n\n
pub fn is_even(num: i32) -> bool {\n    num % 2 == 0\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn test_is_even() {\n        assert_eq!(is_even(4), true);\n        assert_eq!(is_even(7), false);\n    }\n}
\n\n

Let's break down the code piece by piece:

\n\n
    \n
  • #[cfg(test)] — this attribute tells the compiler: "Only compile this module when running tests." It won't be included in the final build.
  • \n
  • mod tests — we create a module for tests. This is standard practice.
  • \n
  • use super::*; — import everything declared above so we can see our is_even function.
  • \n
  • #[test] — a marker that turns a regular function into a test.
  • \n
  • assert_eq! — a macro that checks the equality of two values. If they are not equal, the test fails.
  • \n
\n\n

Run the test with the command:

\n\n
cargo test
\n\n

If everything is done correctly, you will see a green output:

\n\n
running 1 test\ntest tests::test_is_even ... ok\n\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
\n\n

Congratulations, you've written your first Rust test! Now let's figure out how to write more meaningful checks.

\n\n

Basic Macros for Testing

\n\n

In tests, you will constantly use three macros. Remember them:

\n\n
    \n
  • assert!(expression) — checks that the expression is true. If false, the test fails.
  • \n
  • assert_eq!(left, right) — checks the equality of two values. Convenient for comparing results.
  • \n
  • assert_ne!(left, right) — checks that the values are not equal.
  • \n
\n\n

Example using all three macros:

\n\n
pub fn add_two(x: i32) -> i32 {\n    x + 2\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn test_add_two() {\n        assert!(add_two(3) == 5);\n        assert_eq!(add_two(0), 2);\n        assert_ne!(add_two(10), 13);\n    }\n}
\n\n

Note: assert! takes a boolean expression, and

Blogs

Book Recommendations