#panic #should-panic

test_panic

Utility for test cases with panic

8 releases

Uses new Rust 2024

0.4.1 Jul 14, 2025
0.4.0 Jun 22, 2025
0.3.1 Jan 14, 2024
0.3.0 Oct 30, 2023
0.1.0 Jun 30, 2023

#339 in Testing


Used in 2 crates

MIT/Apache

9KB
85 lines

Utility for test cases with panic.

The author of this crate is not good at English. Forgive me if the document is hard to read.

For the same purpose, the shoud_panic attribute is provided in the Rust standard, but it is not so useful, hence we created this crate.

Examples

#[test]
fn test() {
let result = test_panic(|| panic!("message."));
assert!(result.is_panic());
assert!(result.message().contains("message"));
}

test_panic

Utility for test cases with panic.

The author of this crate is not good at English.
Forgive me if the document is hard to read.

What is this?

Provides functions for test with panic. For the same purpose, the shoud_panic attribute is provided in the Rust standard, but it is not so useful, hence we created this crate.

Examples

Example with this crate.

use test_panic::prelude::*;

#[test]
fn test() {
    let result = test_panic(|| panic!("message."));
    assert!(result.is_panic());
    assert!(result.message().contains("message."));
}

Example with should_panic.

#[test]
#[should_panic(expected = "message.")]
fn test() {
    // Suppresses standard error output.
    panic::set_hook(Box::new(|_| {}));
    panic!("message.");
}

Versions

See CHANGELOG.

No runtime deps