简体   繁体   中英

How to print rust log output in tests

My application is using the tracing rust crate to create log output. How do I print these log messages when I'm running my tests?

Usually, if you need to print the output inside the tests you use --nocapture flag with the cargo test command:

$ cargo test -- --nocapture

Can you test if it works in your case?

You can use the tracing_test Rust crate for this. It works like this:

#[traced_test]
#[test]
fn plain_old_test() {
   ...
}

All you have to do is add the tracing_test to your list of dependencies and decorate your tests with the #[traced_test] macro.

This also works with tokio_macros tests, that are decorated with #[tokio::test] .

Then, as @Yuri mentions, you need to run the tests with the --nocapture argument: cargo t --nocapture . This is only relevant if the test succeeds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM