▢️
World 11 Β· Checking Your Work

Running Tests

Imagine you have a whole stack of work and one button that checks every single page at once. In Rust, that button is a command called cargo test. ▢️

The Big Idea The command cargo test finds all the tests in your project, runs them, and tells you which ones passed and which ones failed.

One command, all the checks

You do not run tests one by one. You just type this in the terminal:

cargo test

Cargo rolls up its sleeves, runs every test marked with #[test], and prints a report. When everything is good, it looks like this:

running 2 tests
test it_adds ... ok
test it_subtracts ... ok

test result: ok. 2 passed; 0 failed; 0 ignored

See the word ok at the end of each line? That means that test passed. The last line, test result: ok, is the happy summary β€” every check came back correct. πŸŽ‰

Think of it like this… cargo test is like running through a checklist all at once and ticking off each item: "First check β€” passed! Second check β€” passed!" βœ…

What happens when a test fails?

Nobody is perfect, and that is fine! If your code gives the wrong answer, Rust does not hide it. The test line ends in FAILED instead of ok:

running 2 tests
test it_adds ... ok
test it_subtracts ... FAILED

test result: FAILED. 1 passed; 1 failed; 0 ignored

A FAILED is not a scary thing β€” it is a helpful clue. It points right at the test that broke, so you know exactly where to look and what to fix. πŸ”

Ferris says: A failing test is your code saying "psst, something is off over here!" That is way better than a sneaky bug you never knew about. πŸ¦€
Try this! Next time you are on a computer with Rust set up, type cargo test in a project. Count how many tests say ok!

Quick quiz

A test line ends with the word FAILED. What does that mean?

Exactly! FAILED is a friendly clue pointing you to the spot that needs fixing. πŸ”

You learned… cargo test runs all your tests at once. A line ending in ok passed; one ending in FAILED tells you what to fix. Next up: Organizing Tests β€” where your tests should live! πŸ—ƒοΈ