-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
eprintln prints to stdout in a test #126420
Comments
Could you expand on the reason this makes a difference? Also, does this reproduce with |
If you use other libraries that actually do print to stderr, and you see that output showing up, while the outputs of eprintln are missing, you'll (wrongly) assume that the eprintln calls were not executed, and that your test contains some strange bugs that mess up the control flow badly. If you know about this behavior, it is much less of a problem, and using --nocapture makes it also less of a problem. However, neither is the case by default. |
So you know which stream the underlying production code is meant to write to and you are splitting the streams (e.g. Or are you not wanting btw as a point of explanation, I don't think any of this is being done by Cargo but by the built-in test harness, "libtest", which is why it isn't documented in Cargo. The way stdout/stderr capture works is a bit hacky and plugs directly into the Going ahead and moving this over to libtests repo |
I just realized, that I messed up the strings in the example. These lines: _ = writeln!(
std::io::stderr(),
"This should REALLY go to stdout (bad test)"
); should of course have the string
With this solution, it would be nice to additionally have the arguments And the situation that caused this problem was the following: I have cli programs that print some stuff to stderr and some to stdout. I wrote an integration test that executes the programs, and captures their stdout streams, while just forwarding their stderr streams. Additionally, the test itself uses eprintln for debugging output. The stderr output of the programs ended up in between the test results report while the eprintln output of the test was missing. This was what caused me a lot of confusion. |
To be clear, are (1) and (2) alternative solutions you would expect to see? And to put these two words
with an optional
Missing or was captured and not reported because the test didn't fail? This is a |
Yeah, I guess I have more or less the same issue as the person that opens the issue that you linked. I would consider 1. a minimal solution, and 2. the better solution. The current state is that you actually don't capture stdout (as I just tested) or stderr. All you do is redirecting the output of the The ideal state, in my opinion, would be to not fiddle with the macros, and just let them print to stderr and stdout as they are supposed to, and as everyone expects them to, and then actually capture stderr and stdout. Then you would have consistent behavior. If you want to see the output of your program you can use |
If this is about #90785, then I'm inclined to close in favor of that.
Thats an explanation of what they do. I'm looking for use cases to understand why someone would need to specifically not capture only one stream. |
The use case depends on the test. If you write an integration test that calls multiple other programs, and that has a bug itself, sometimes you might be interested in the program's debug output, which typically goes to stderr. Sometimes you might be interested in the programs' stdout, an example might be seeing some hashes that md5sum prints to stdout. Sometimes you might want to see both at the same time. Also, #90785 is only about tests not capturing stdout. This is additionally about tests not capturing stderr, and tests redirecting print and eprint, and about the fact that this is undocumented. |
I've updated the title for #90785. |
Problem
In a test,
eprintln
prints to stdout.Steps
cargo test
and observe output, which, btw, looks like this:Possible Solution(s)
Make
eprintln
print to stderr. If the current behavior is actually intended, it should at least be documented, but isn't (I checked the cargo test docs, and the eprintln docs)Notes
I think by default, a test should capture stdout and stderr, but not just silently redirect the
eprintln
macro.Version
EDIT (one day later): I just realized that I messed up the strings. The String
"This should REALLY go to stdout"
should of course be"This should REALLY go to stderr"
, with the same applying to the bad test variant.The text was updated successfully, but these errors were encountered: