#expect #macro #assert #test-macro #result #test

expecting

Rust 宏,用于测试条件而不引发 panic

9 个版本 (5 个破坏性更改)

0.6.0 2024 年 5 月 2 日
0.5.0 2024 年 4 月 9 日
0.4.0 2023 年 1 月 14 日
0.3.0 2023 年 1 月 11 日
0.1.1 2022 年 12 月 18 日

#145 in 测试

Download history 2/week @ 2024-04-18 4/week @ 2024-04-25 142/week @ 2024-05-02 5/week @ 2024-05-16 3/week @ 2024-05-23 1/week @ 2024-06-06 2/week @ 2024-06-20 2/week @ 2024-06-27 3/week @ 2024-07-04

每月 557 次下载
用于 muffin

MIT 许可证

34KB
432

Expecting

Crates.io Documentation Crates.io

Expecting 提供用于测试条件而不引发 panic 的宏。当预期的条件不满足时,expect_* 宏系列会引发 anyhow::Error

描述
expect!(condition) 期望 conditiontrue
expect_eq!(a,b) 期望 a == b
expect_ne!(a,b) 期望 a != b
expect_some(option) 期望 optionSome(x) 并返回 x
expect_some_eq(some_a,a) 期望 some_a == Some(a)
expect_none(option) 期望 optionNone
expect_ok(result) 期望 resultOk(x) 并返回 x
expect_err(result) 期望 resultErr(e) 并返回 e
expect_contains(string,substr) 期望 string 包含 substr
expect_contains(container,element) 期望 container (例如,Vec) 包含 element
expect_empty(container) 期望 container 没有元素。
expect_empty(string) 期望 string 的长度为零。
expect_not_empty(container) 期望 container 有 1+ 个元素。
expect_not_empty(string) 期望string的长度不为零。

该包在涉及资源分配和拆除的异步集成测试中特别有用;你不必费劲去捕获恐慌,只需简单地使用expect_*代替assert_*来返回Result即可。

示例

use expecting::expect_eq;

#[test]
fn passing_test() -> Result<()> {
    expect_eq!(1, 1);
    Ok(())
}

#[test]
fn failing_test() -> Result<()> {
    expect_eq!(1, 2);  // returns early
    Ok(())  // won't be reached
}

失败的测试的错误信息包括行号和详细描述。

expect_eq error

expect_contains error

有关使用示例和更多信息,请参阅文档

依赖关系

约130KB