#[test]
#[cfg(test)]
cargo build
#[cfg(test)] mod tests { #[test] fn it_works() { let result = 2 + 2; assert_eq!(result, 4); } }
assert!
assert!(larger.can_hold(&smaller));
assert_eq!
assert_eq!(4, add_two(2));
assert!( result.contains("Carol"), "Greeting did not contain name, value was `{}`", result );
should_panic
#[cfg(test)] mod tests { use super::*; #[test] #[should_panic] fn greater_than_100() { Guess::new(200); } #[test] #[should_panic(expected = "less than or equal to 100")] fn greater_than_100() { Guess::new(200); } }
pub fn add_two(a: i32) -> i32 { internal_adder(a, 2) } fn internal_adder(a: i32, b: i32) -> i32 { a + b } #[cfg(test)] mod tests { use super::*; # 이 트릭(?)을 통해 private 함수에 접근 #[test] fn internal() { assert_eq!(4, internal_adder(2, 2)); } }