test_panic

Crates.iotest_panic
lib.rstest_panic
version0.7.0
created_at2023-06-30 05:42:23.373536+00
updated_at2026-01-12 16:04:52.451763+00
descriptionUtility for test cases with panic.
homepage
repositoryhttps://github.com/nossie531/test_panic
max_upload_size
id904130
size52,487
(nossie531)

documentation

README

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, shoud_panic attribute is provided in the Rust standard, but it is not so useful, hence we created this crate.

Example 1

Example with always panic.

use test_panic::prelude::*;

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

Following is equivalent code with should_panic.

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

Example 2

Example with multiple tests (This cannot be done with should_panic).

use test_panic::prelude::*;

#[test]
fn with_multi_tests() {
    let datas = [
        ((10, 3), ok(3)),
        ((10, 0), ng()),
        ((10, 15), msg("Result is too small")),
    ];

    for ((x, y), tobe) in datas {
        let asis = test_panic(|| divide(x, y));
        assert_eqa!(asis, tobe);
    }
}

fn divide(x: i32, y: i32) -> i32 {
    assert!(y > 0);
    assert!(x / y >= 1, "Result is too small");
    x / y
}

History

See CHANGELOG.

Commit count: 15

cargo fmt