Test
Overall, Rust provides a relatively simple but comprehensive testing facility that suits a test-driven development model. Unfortunately, at the author’s workplace, most release cycles have too few people and too many requirements, so test-driven development is not practical. Adding unit and integration tests later, when there is finally time for testing, becomes very troublesome.
Comprehension
Basic syntax
pub fn greeting(name: &str) -> String { String::from("Hello!") } pub struct Guess { value: i32, } impl Guess { pub fn new(value: i32) -> Guess { if value < 1 || value > 100 { panic!("Guess value must be between 1 and 100, got {}.", value); } Guess { value } } } #[cfg(test)] mod tests { use super::*; #[test] fn it_works() { // assert_ne! is also available assert_eq!(2 + 2, 4); } #[test] fn will_fail() { panic!("Make this test fail"); } #[test] fn greeting_contains_name() { let result = greeting("Carol"); // print a hint when an assertion does not meet expectations assert!( result.contains("Carol"), "Greeting did not contain name, value was `{}`", result ); } #[test] #[should_panic] // should_panic checks whether a panic occurs fn greater_than_100() { Guess::new(200); } } #[cfg(test)] mod tests2 { #[test] // when returning Result<T, E>, the should_panic attribute cannot be used fn it_works() -> Result<(), String> { if 2 + 2 == 4 { Ok(()) } else { Err(String::from("two plus two does not equal four")) } } }