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