Skip to content
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

libtest - capture stderr from tests #40298

Closed
NoraCodes opened this issue Mar 6, 2017 · 10 comments
Closed

libtest - capture stderr from tests #40298

NoraCodes opened this issue Mar 6, 2017 · 10 comments
Labels
A-libtest Area: #[test] related C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@NoraCodes
Copy link
Contributor

NoraCodes commented Mar 6, 2017

I'm currently having a problem with testing libraries that use log with either env_logger or flexi_logger in their tests. In the "real world", they should log to stderr, but libtest (and, by extension Cargo) doesn't capture stderr from tests, so I have to jump through hoops to get these libraries to log to stdout instead, or sift through huge logdumps.

Would it be possible to capture stderr from tests?

I originally posted this on the Cargo repo, but was told to file here. Possibly related to #31983, but I've tried limiting the test threads (RUST_TEST_THREADS=1) with no result.

@martinlindhe
Copy link
Contributor

martinlindhe commented Mar 31, 2017

I agree on this.

For a little more detail;

Consider the following

#[macro_use]
extern crate log;

fn do_stuff() -> bool {
    println!("println");
    error!("error");
    true
}

#[test]
fn my_test() {
    assert_eq!(true, do_stuff());
}

run: cargo test -- --nocapture

actual output:

running 1 test
println
test my_test ... ok

expected output:

running 1 test
println
error
test my_test ... ok

@dpc
Copy link
Contributor

dpc commented Mar 31, 2017

Using slog-envlogger (and other slog crates) one can easily have a logger that logs to stderr, stdout, file... whatever, with colors, in one of several format and if combine it with control of RUST_LOG variable. Just saying...

@martinlindhe
Copy link
Contributor

@dpc Not sure it would help, since it seems cargo test, even with the --nocapture swallows stderr. Only stdout is shown.

@dpc
Copy link
Contributor

dpc commented Mar 31, 2017

@ martinlindhe ... Edited my original coment. Slog doesn't care. Log to stdout, stderr, file. Or even open new raw 1 file descriptor and circumvent cargo test jail.

@dpc
Copy link
Contributor

dpc commented Mar 31, 2017

I actually should create slog-test crate specifically for that purpose... Ping me on slog gitter if you're interested. I guess it's enough of slog advertising. :D

@martinlindhe
Copy link
Contributor

martinlindhe commented Mar 31, 2017

@dpc, Okay.. so I can make a custom slog logger that outputs to stdout, thus allow cargo test to fetch the output. Gotcha.
Will also checkout slog when time permits!
But I think it would benefit the end user more if cargo test would display stderr, even if it is hidden behind a rather obscure flag as --nocapture.
Especially considering the log crate is the default logger as documented on rust-lang.org

@NoraCodes
Copy link
Contributor Author

NoraCodes commented Mar 31, 2017 via email

@elichai
Copy link
Contributor

elichai commented Jan 31, 2019

Any updates on this?
Does this mean that right now using rust log I can't see them in tests?

@vincentdephily
Copy link
Contributor

AFAIU libtest now captures "stderr" and "stdout" but does so in a way that isn't compatible with some loggers that try to use the standard stderr/stdout without using something like println!(). For example rust-cli/env_logger#107 is discussing workarounds but it has drawbacks.

Another minor issue is that while libtest does capture println!() and eprintln!() output, they are both output on and labeled as "stdout" only.

@Mark-Simulacrum
Copy link
Member

Closing as duplicate of #42474.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-libtest Area: #[test] related C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

6 participants