1 /*! 2 Using `env_logger` in tests. 3 4 Log events will be captured by `cargo` and only printed if the test fails. 5 You can run this example by calling: 6 7 ```text 8 cargo test --example in_tests 9 ``` 10 11 You should see the `it_does_not_work` test fail and include its log output. 12 */ 13 main()14fn main() {} 15 16 #[cfg(test)] 17 mod tests { 18 use log::debug; 19 init_logger()20 fn init_logger() { 21 let _ = env_logger::builder() 22 // Include all events in tests 23 .filter_level(log::LevelFilter::max()) 24 // Ensure events are captured by `cargo test` 25 .is_test(true) 26 // Ignore errors initializing the logger if tests race to configure it 27 .try_init(); 28 } 29 30 #[test] it_works()31 fn it_works() { 32 init_logger(); 33 34 let a = 1; 35 let b = 2; 36 37 debug!("checking whether {} + {} = 3", a, b); 38 39 assert_eq!(3, a + b); 40 } 41 42 #[test] it_does_not_work()43 fn it_does_not_work() { 44 init_logger(); 45 46 let a = 1; 47 let b = 2; 48 49 debug!("checking whether {} + {} = 6", a, b); 50 51 assert_eq!(6, a + b); 52 } 53 } 54