Test Runners
Recipe | Crates | Categories |
---|---|---|
Test your code with cargo test | ||
Test your code Faster with cargo nextest |
FIXME
Test your code with cargo test
cargo test
to run all tests.
cargo test test_prefix
to run all tests that start with the provided prefix.
cargo test -- --show-output
to show output (println!) that is otherwise captured during tests.
struct Rectangle { width: u32, height: u32, } impl Rectangle { fn can_hold(&self, _another: &Rectangle) -> bool { true } } // Put unit tests in the same file than the main code #[cfg(test)] // only for unit tests mod tests { // Access to all objects in the parent module, // which contains the main code use super::*; // Test functions must be free, monomorphic functions that take no // arguments, and commonly return () or Result<T, E> where T: // Termination, E: Debug #[test] fn larger_can_hold_smaller() { let larger = Rectangle { width: 8, height: 7, }; let smaller = Rectangle { width: 5, height: 1, }; assert!(larger.can_hold(&smaller)); // You may also use: assert_eq!(result, some_const); // or assert_ne!(...) } // This test passes if the code inside the function panics; // It fails if the code inside the function doesn't panic. #[should_panic] #[test] fn another() { panic!("Make this test fail"); } // With Result #[test] fn it_works() -> Result<(), String> { if 2 + 2 == 4 { Ok(()) // Pass if OK } else { Err(String::from("two plus two does not equal four")) } } #[ignore = "This test takes an hour to run. Only run it manually when needed"] #[test] fn expensive_test() { // Long-running code } }
Test your code Faster with cargo nextest
cargo-nextest
⮳ is a new, faster test runner for Rust.
cargo nextest run
cargo test --doc