Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize the logger for integration tests?

Tags:

rust

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?

like image 1000
Dmitry Uvarov Avatar asked May 11 '15 21:05

Dmitry Uvarov


1 Answers

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.

like image 61
Danilo Bargen Avatar answered Sep 27 '22 21:09

Danilo Bargen