πŸ—ƒοΈ
World 11 Β· Checking Your Work

Organizing Tests

When you organize a backpack, you do not throw everything in one pocket β€” books in one spot, snacks in another. Tests like to be organized too! There are two main places they live. πŸ—ƒοΈ

The Big Idea Unit tests live right next to the code they check. Integration tests live in their own special folder and test your whole program from the outside.

Unit tests: close to home

A unit is one small piece of your code, like a single function. Unit tests sit in the same file as that code, tucked inside a little box called a mod tests. Above the box you write #[cfg(test)].

That tag means β€œonly build this part when we are testing.” So your tests come along during cargo test, but they do not get packed into the real program. Neat and clean! 🧹

fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_adds() {
        assert_eq!(add(2, 3), 5);
    }
}
Think of it like this… A unit test is like checking each LEGO brick to make sure it snaps shut properly β€” one small piece at a time, right where it sits. 🧱

Integration tests: the big picture

Sometimes you want to test the whole model after it is built, not just one brick. Those are integration tests, and they live in their own folder named tests/, sitting next to your code folder.

Files in there test your program the same way a real user would β€” from the outside, pressing the buttons and checking the results.

Ferris says: Unit tests check the tiny pieces. Integration tests check that all the pieces work together. You usually want both! πŸ¦€
Try this! Look at the spot the tests live. Unit tests share a file with #[cfg(test)]; integration tests get their own tests/ folder. Can you remember which is which?

Quick quiz

Where do integration tests live?

Right! Integration tests get their own tests/ folder and check everything working together. πŸ—ƒοΈ

You learned… Unit tests sit next to your code in a #[cfg(test)] mod tests box, while integration tests live in a separate tests/ folder and check the whole program. You have finished World 11 β€” great work checking your work! πŸŽ‰