I have a crate with production code in the src directory and integration tests in the tests directory. The production code uses log macros.
I would like to init a global logger when running the integration tests (e.g. env_logger::init().unwrap();) There are several tests and the test order is not defined, so I don't know in which test I should put the initialize command.
Is there any way I can do this nicely? Perhaps by overriding the tests main function?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With