-
Notifications
You must be signed in to change notification settings - Fork 95
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
Log capturing in unit tests #256
Comments
You need to use |
I might be wrong, but I think tests in Rust are not intercepting stdio, but only calls to |
Ha. It's in |
What's the point of writing all this documentation when no one reads it. :D https://docs.rs/slog-term/2.5.0/slog_term/struct.TestStdoutWriter.html |
Right, I thought I got it working but I failed to run the test… Thanks! |
Also, for future reference because I just fixed this, I needed to use let logger = {
let decorator = slog_term::PlainDecorator::new(slog_term::TestStdoutWriter);
let drain = Mutex::new(slog_term::FullFormat::new(decorator).build()).fuse();
slog::Logger::root(drain, o!())
}; |
Async should work too, no? Particularly in |
It works, yes, but I get all the console output that I don’t want for succeeding unit tests. With the |
Really? Why? Are you using both |
Yes, the variant that works good for me is this: #[tokio::test]
async fn test_things() -> Result<()> {
let logger = {
let decorator = slog_term::PlainDecorator::new(slog_term::TestStdoutWriter);
let drain = Mutex::new(slog_term::FullFormat::new(decorator).build()).fuse();
slog::Logger::root(drain, o!())
};
info!(logger, "Hi from async test");
Ok(())
} |
I know this has been closed for a while, but I'd like to save other people some headache and time, trying to figure out why this doesn't work with TL;DR: Rust's testing framework uses thread locals to mark that output should be captured, so creating any threads in a test case and passing a logger (or just even calling just Perhaps this could be mentioned in the docs at some point? |
Please fill a PR! |
There you go: slog-rs/term#32 |
Thanks! |
I'm using the following for unittests in slog 2.7.0, but I really miss the nice colored output I get elsewhere
I can't find a way to get the colors back. Does anyone have any ideas? |
@purew https://docs.rs/slog-term/2.8.0/slog_term/struct.TermDecoratorBuilder.html#method.force_color to make it not detect that it's not printing to |
Hmm, how would I combine this with the code I pasted above? |
Use |
When writing unit tests, cargo captures output to stdout by default.
This will not print the text to the terminal, unless
cargo test
is started with the-- --nocapture
argument.However, with slog this does not seem to work:
Running this with
cargo test
will print:Neither stdout nor stderr logs are captured.
I realize that this is probably due to some pecularity in how cargo handles capturing, but is there some kind of drain that logs in a cargo log capturing compatible way (possibly using
println
) that can be used in tests?The text was updated successfully, but these errors were encountered: