#assertions #failure #fail #expect

soft-test-failures

允许一个测试中多个断言失败

2个不稳定版本

0.2.0 2019年5月23日
0.1.0 2018年11月7日

#793 in 测试

MIT/Apache

5KB
58

Soft-Test-Failures

多个测试失败,其中任何一个都会导致测试失败。类似于gtest的EXPECT_*而不是ASSERT_*

#[test]
fn expect_failures() {
    let x = 4;
    let y = "is not";
    let z = 5;
    expect!(2 + 2 == 5, "{} surely {} {}", x, y, z);
    expect!(1 + 1 == 2);
    expect!(3 - 7 == -4);
    expect!(3 - 7 == -3);
    let_fail!();
}

running 1 test
test expect_failures ... FAILED

failures:

---- expect_failures stdout ----
thread 'expect_failures' panicked at '`expect` test failed with 2 failed assertions:
1: 4 surely is not 5
2: Expected 3 - 7 == -3
', src/lib.rs:48:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.


lib.rs:

这个crate跟踪测试执行过程中的断言,延迟panic直到执行结束。这确保了如果两个断言都失败了,第一个不会覆盖第二个。这在编写包含许多断言的大型测试时非常有用,因为你可以精确地指出哪些失败了。

用法

用此crate中的expect!()替换你的常规assert!()断言。在测试结束时,调用let_fail!()

#[test]
fn expect_failures() {
    let x = 4;
    let y = "is not";
    let z = 5;
    expect!(2 + 2 == 5, "{} surely {} {}", x, y, z);
    expect!(1 + 1 == 2);
    expect!(3 - 7 == -4);
    expect!(3 - 7 == -3);
    let_fail!();
}

无运行时依赖