Console log during tests execution

Hi,

I am using log crate and I would like to see (in console) my log messages during tests execution.
How I can do that?

Thank you

By default cargo captures the output and then prints the output if the test fails. To prevent it from capturing the output, you can use the --nocapture flag like so:

cargo test <other flags if you want> -- --nocapture
2 Likes

There is still one question: how I can initialize my log just before to execute my tests? So something like a setup function.

P.S.:
I am using Pretty Env Logger

You need to call try_init() in every test.

Argh... So there is no way/trick to have a kind of setup method to be called before executing every test?

You could write your own proc-macro similar to how #[tokio::test] was implemented. Besides that, no.

2 Likes

You only need to run the setup code once, so why not try using the ctor crate to initialize the logger on startup?

#[ctor]
fn global_setup() {
  pretty_env_logger::init();
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.