How to initialize the logger for integration tests?

后端 未结 6 1192
伪装坚强ぢ
伪装坚强ぢ 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:36

    You can use something like this:

    use std::sync::Once;
    
    static INIT: Once = Once::new();
    
    /// Setup function that is only run once, even if called multiple times.
    fn setup() {
        INIT.call_once(|| {
            env_logger::init().unwrap();
        });
    }
    

    Then simply call setup() in the beginning of each test.

    Originally based on this blogpost.

提交回复
热议问题