Skip to main content

Crate test_panic

Crate test_panic 

Source
Expand description

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

§Examples

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"));
}

Example with multiple tests.

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
}

Modules§

msg
Crate’s messages.
prelude
Crate’s prelude.

Macros§

assert_eqa
Asserts that two expressions are almost equal (using eq_almost).
assert_eqn
Asserts that two expressions are nearly equal (using eq_nearly).

Enums§

TestPanicResult
Result of test_panic function.

Functions§

msg
Shorthand of TestPanicResult::Panic with payload.
ng
Shorthand of TestPanicResult::Panic without payload.
ok
Shorthand of TestPanicResult::Cool.
test_panic
Execute the closure and get its return value or panic information.