Should_panic control

is there a way of controlling which statement should cause a panic in my test ?

Most of my test should run normally, and any panic should be a test failure, but in general I want the should_panic directive to work only on the last statement.

No, but you can specify the expected panic message.

#[test]
#[should_panic(expected = "my panic message")]
fn panic() {
    panic!("my panic message");
}

https://doc.rust-lang.org/reference/attributes/testing.html#the-should_panic-attribute

Alternatively re-implement should-panic internally in your test, so you have more control over exactly what it tests

#[test]
fn panic() {
    setup();
    let res = std::panic::catch_unwind(|| under_test());
    assert!(res.is_err());
}
3 Likes

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.