-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Clearer error for failed tests which returned a Result Err() #69517
Comments
Thanks for the report! I've transferred this to rust-lang/rust instead of rust-lang/cargo since I think it's more about libtest which lives in the rust repo. |
There was a PR to improve this (#52453), but perhaps it didn't go far enough. I can see how it can be confusing, but I'm not sure what a better output would look like. |
Here's some example output when a test panics by using
Good:
Bad:
Here's the same test, where
Good:
Bad:
I think something that shows
and no extraneous panic information would be ideal. Something like:
Location of the error is always in the same place so I don't have to scan the screen for it. The second line gives me the human readable error, the third line gives me the error type in case I need to go poking through the code to see where this error can be generated |
It's kinda' sorta' explained here: <rust-lang/rust#69517>. Basically, though, tests that return a `Result<_>` will have less helpful backtraces than tests that just use asserts and `unwrap()/expect(_)` all over the place. I recently ran into an undiagnosable test failure that this commit makes diagnosable, so this is now "the way things are done" for this project, going forwards.
It's kinda' sorta' explained here: <rust-lang/rust#69517>. Basically, though, tests that return a `Result<_>` will have less helpful backtraces than tests that just use asserts and `unwrap()/expect(_)` all over the place. I recently ran into an undiagnosable test failure that this commit makes diagnosable, so this is now "the way things are done" for this project, going forwards.
It's kinda' sorta' explained here: <rust-lang/rust#69517>. Basically, though, tests that return a `Result<_>` will have less helpful backtraces than tests that just use asserts and `unwrap()/expect(_)` all over the place. I recently ran into an undiagnosable test failure that this commit makes diagnosable, so this is now "the way things are done" for this project, going forwards.
I ran into this myself when starting to write some tests, and it was baffling. Since I had specifically written a test that doesn’t panic, and the failed test result prominently showed a complicated panic in the test runner internals, I ruled out the possibility of this being intended behaviour. I could only conclude that I must be triggering some kind of obscure bug in the runner, and spent a lot of time trying to track it down. |
This surprised me as well, the "returned a termination value with a non-zero status code" error message is outright unhelpful. Adding boilerplate like: #[test]
fn wrapped() {
fn wrapper() -> Result<(), std::io::Error> {
let f = std::fs::File::open("/root/nonexistantfile")?;
assert_eq!(example_method(&f), 0);
Ok(())
}
wrapper().unwrap();
} results (IMO) in a clearly better failure message than the default behavior. Playground example. |
…t 2018's support for Result-returning tests. This also adds a dependency on anyhow, to simplify result propagation and avoid requiring users to specify a particular Result-type. Due to rust-lang/rust#69517 we manually unwrap() the user-provided Result for the time being. When that issue is resolved the implementation can be simplified slightly.
Since rust
|
Sidenote: There is a crate |
Problem
I have a test function which returns a
Result<...>
as a convenient way of testing failable methods. For example:The test started failing for whatever reason, and the
cargo test
output looked like this:Not reading the output properly, I interpreted the
panicked at 'assertion failed: ...
as being a failure from myassert_eq!
and started trying to work out why myexample_method
was returning 0.I also tried running the tests under a debugger, but the backtrace only pointed at the test closure rather than the probelmatic line:
..where src/main.rs:6 is the line
fn exampletest() -> Result<(), std::io::Error> {
Steps
Result<...>
Err(...)
Possible Solution(s)
I think there are two aspects to this:
First is the panic message from the assert is talking about "internal" details - the exit code of the test. The fact the test (I assume) runs as a process and the exit-code is used to determine failure isn't something I was thinking about while writing/debugging the test.
For this, I think it would help a lot to just simplify the panic message from saying
to something more concise like:
Second part I'm not so clear about, but it seems like the Result isn't being handled specially - the
Err
is just being turned into an exit code, then asserted later. Since the panic comes from this assertion, the backtrace doesn't reference the actual line which failed.This is different to if I had, say, used
.unwrap()
instead of?
:where line 7 is the problem-causing line
let _f = std::fs::File::open("/root/nonexistantfile").unwrap();
- it would be nice if using?
resulted in a similar tracebackNotes
Output of
cargo version
:The text was updated successfully, but these errors were encountered: