Rust :: Testing

Testing

Unit Testing

#[cfg(test)]
mod tests {
    // use this line to access private functions
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    #[should_panic(expected = "failure message")]
    fn pass_when_panic() {
        assert_ne!(expression) // pass if not equal
    } 

    #[test]
    #[ignore]
    fn ignore_for_now() {
        assert!(exp,"failure message")
    }

    // cannot use #[should_panic]    
    #[test]
    fn return_result() -> Result<(), String> {
        if 2 + 2 == 4 {
            Ok(())
        } else {
            Err(String::from("some error"))
        }
    }
}  

Integration Tests

// in `my_test.rs`
use foo;
mod my_other_test;

#[test]
fn some_fn() {
    my_other_test::some_fn();
    assert!(some_exp);
}
foo
├── Cargo.toml
├── src
│   └── main.rs
│   └── lib.rs
└── tests
    ├── my_test.rs
    └── my_other_test
            └── mod.rs