Skip to content

Latest commit

 

History

History
50 lines (42 loc) · 1.26 KB

File metadata and controls

50 lines (42 loc) · 1.26 KB

Unit Testing in Rust

Explore: Home Basics

Cheatsheet

  • cargo test <testname_substring?>
  • cargo test -- --show-output
  • cargo test -- --ignored
  • cargo test -- --test-threads=1

Example

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

    #[test]
    fn happy_path() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }

    #[test]
    fn greeting_contains_name() {
        let result = greeting("Carol");
        assert!(
            result.contains("Carol"),
            "Greeting did not contain name, value was `{result}`"
        );
    }

    #[test]
    #[should_panic(expected = "less than or equal to 100")]
    fn greater_than_100() {
        Guess::new(200);
    }

    #[test]
    #[ignore]
    fn expensive_test() {
        // code that takes an hour to run
    }
}

Resources