Re: Test, should_panic: somehow hide stack traces?

Hello, my question has already been asked + answered in this post:

But I think this is not a sufficient answer. It hides all panic stack traces, but I want to do that only for the specific panic I want to test for.

[should_panic = "foo"]

I tried to create a custom panic hook like this:

fn should_panic(contains: &'static str){ //call at the first line of a `should_panic` unit test
    let default_hook = take_hook();
    set_hook(Box::new(move |panic_info|{
        if !matches!(panic_info.payload().downcast_ref::<&str>(), Some(msg) if msg.contains(contains)){
            default_hook(panic_info);
        }
    }));
}

But this does not seem to work, it causes my unit test to fail even with the correct panic.

What could I do? Shouldn't this be the default behaviour of should_panic unit tests?

If tests succeed, they shouldn't show any output (including any backtraces) unless you use --nocapture or --show-output when running the test.

$ echo '#[should_panic] #[test] fn foo() { panic!(); }' | rustc - --test
$ ./rust_out

running 1 test
test foo - should panic ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.