How to initialize the logger for integration tests?

后端 未结 6 1213
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 18:07

I have a crate with production code in the src directory and integration tests in the tests directory. The production code uses log ma

6条回答
  •  一个人的身影
    2020-12-01 18:43

    The latest documentation has a recommendation:

    #[cfg(test)]
    mod tests {
        fn init() {
            let _ = env_logger::builder().is_test(true).try_init();
        }
    
        #[test]
        fn it_works() {
            init();
            info!("This record will be captured by `cargo test`");
    
            assert_eq!(3, 1 + 2);
        }
    }
    

提交回复
热议问题