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. βΆοΈ
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. π
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. π
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. π
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! ποΈ