Why Testing in Rust Is Not Scary, But Interesting?
\n\nWhen 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\nMany 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\nIn 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\nWhat You Need to Know Before Starting?
\n\nBefore writing tests, make sure you have Rust installed. If not, open a terminal and run:
\n\ncurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\n\nEverything 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\nLet's create a new project for experiments:
\n\ncargo new rust_test_guide\ncd rust_test_guide\n\nGreat! Now we have a clean project where we will learn to write tests.
\n\nFirst Test: Understanding with an Example
\n\nOpen the file src/lib.rs. By default it is empty, but we'll add a simple function and a test for it.
Let's write a function that checks whether a number is even:
\n\npub 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\nLet'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 ouris_evenfunction. \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
Run the test with the command:
\n\ncargo test\n\nIf everything is done correctly, you will see a green output:
\n\nrunning 1 test\ntest tests::test_is_even ... ok\n\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out\n\nCongratulations, you've written your first Rust test! Now let's figure out how to write more meaningful checks.
\n\nBasic Macros for Testing
\n\nIn 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
Example using all three macros:
\n\npub 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\nNote: assert! takes a boolean expression, and