Rullst AcademyRullst Academy/Lesson
Rust II: data, choices and confidence · Lesson 11 / 12

Tests that find defects

A useful test checks a rule, including where it changes. Let us find a one-character defect.

Learning paths14 min

Let's discover together

  1. 1

    #[test] marks functions run by cargo test. #[cfg(test)] includes the module only in test builds. use super::* lets tests access items from their parent module.

  2. 2

    assert! requires true; assert!(!value) requires false. assert_eq! compares values. A failure should describe an expectation from the rule, not copy the implementation calculation.

  3. 3

    With 3 available, requests for 2 and 3 are allowed; 4 is not. Zero is also invalid. These cases exercise below, exactly at, above and a forbidden value.

  4. 4

    Scenario 2 replaces <= with < and rejects a valid request for 3. Run tests after the change: boundary should fail. Restoring the rule and seeing it pass shows the test detects that specific defect.

FROM LESSON TO PRACTICE

A guide to follow at your pace

Read the steps before using the reference below. Commands run in the indicated system’s terminal; signup happens on the official site. Rullst Academy exercises are practice and do not verify your installation or external account.

fn can_take(available: u16, request: u16) -> bool {
    request > 0 && request <= available
}
fn main() {
    println!("{} {} {}", can_take(3, 2), can_take(3, 3), can_take(3, 4));
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn boundary() {
        assert!(can_take(3, 2));
        assert!(can_take(3, 3));
        assert!(!can_take(3, 4));
    }
    #[test]
    fn rejects_zero() { assert!(!can_take(3, 0)); }
}

Official links and next step

✦
YOUR MISSION

Write boundary tests and show they detect a faulty change.

Now it's your turn

Discovery workshop

Read, predict, investigate

Predict before revealing. This panel shows prepared results; it does not compile your code. Compiler errors are summarised; exact wording can vary with the compiler version.
Mini experiment

Learning simulation: compare cards and check your predictions. The browser is not compiling Rust or starting a server.

fn can_take(available: u16, request: u16) -> bool {
    request > 0 && request <= available
}
fn main() {
    println!("{} {} {}", can_take(3, 2), can_take(3, 3), can_take(3, 4));
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn boundary() {
        assert!(can_take(3, 2));
        assert!(can_take(3, 3));
        assert!(!can_take(3, 4));
    }
    #[test]
    fn rejects_zero() { assert!(!can_take(3, 0)); }
}
Build your answers and check when you are ready.
A moment to understand

Do all passing tests prove there are no defects?

I need a hint

A finite set of examples does not automatically cover every behaviour.

Loading your journey…

Take it with you

Run cargo test. Replace <= with <, confirm failure and restore it. Add cases (0, 1) and (20, 20), explaining the rule each protects.

RUST II · .rs

Your Rust II workbench

Read, predict and answer here. To actually compile, extract the activity ZIP into a new folder, open a terminal beside Cargo.toml and use the commands below. Rust and your operating system tools must be installed.

cargo check --offline
cargo test --offline
cargo run --offline
My files and GitHub

The editor below saves a draft in this browser and lets you download or publish your .rs. To run it in the project, save the edited file as src/main.rs. The ZIP contains the starter version; prepared results do not change with your edits. Online grading does not verify local execution.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.