Skip to content

Could log output be shown in tests? #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
martinlindhe opened this issue Mar 30, 2017 · 5 comments
Closed

Could log output be shown in tests? #106

martinlindhe opened this issue Mar 30, 2017 · 5 comments

Comments

@martinlindhe
Copy link

Normally running tests mutes standard output, but using this rather obscure command cargo test -- --nocapture, output such as println!("something"); is shown in the test result.
This is great for debugging. However, it doesn't appear to work with the log macros.

I found #56 which documents how to enable output in tests specifically, but I am wondering if there is some easier way?

@martinlindhe martinlindhe changed the title Could log output be shown in tests, given accept Could log output be shown in tests? Mar 30, 2017
@sfackler
Copy link
Member

This seems like more of a rust-lang/rust issue than this crate - log doesn't touch println at all.

@martinlindhe
Copy link
Author

@sfackler Thanks for clarifying! I think I got confused by the documentation.

@martinlindhe
Copy link
Author

Posting for reference: After some digging, I found a report of this already at rust-lang/rust#40298

@cbarber
Copy link

cbarber commented Jul 30, 2020

Leaving this for google results. A decent work around:

#[cfg(not(test))]
use log::{info, warn};
 
#[cfg(test)]
use std::{println as info, println as warn};

bors bot added a commit to smoltcp-rs/smoltcp that referenced this issue Nov 3, 2021
563: Print log to stdout in test r=Dirbaio a=deantvv

Use simple [trick](rust-lang/log#106) to allow print log to stdout in test

Co-authored-by: Dean Li <deantvv@gmail.com>
Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
EFanZh pushed a commit to EFanZh/log that referenced this issue Jul 23, 2023
On windows, but also use templating to render it correctly everywhere.

Fixes rust-lang#106
Fixes rust-lang#111
@AlejandroFNadal
Copy link

AlejandroFNadal commented Jun 6, 2024

For people searching and finding this issue:

Add in each test, instead of
#[test]
put:

#[test_log::test]

Then, to cargo toml

[dev-dependencies]
test-log = "*"

Finally, set env variable to desired debugging level:
export RUST_LOG="debug"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants