5 个版本
使用旧的 Rust 2015
0.0.5 | 2021年2月21日 |
---|---|
0.0.4 | 2018年9月30日 |
0.0.3 | 2018年9月30日 |
0.0.2 | 2018年9月30日 |
0.0.1 | 2018年9月30日 |
#252 在 测试
166 每月下载量
12KB
151 行
这是一个小型的库,它可以在 Rust 中启用段落式测试。段落式测试使得编写许多类似的测试用例变得简单、自然且简洁。
每个顶级段落测试都会重复运行,对于测试中的每个唯一的段落运行一次。这比固定设置更具有表现力和自然,因为它允许你在段落内部使用父作用域中的局部变量,并且你可以将段落嵌套到任意深度。
入门指南
该库发布在 https://crates.io/crates/section_testing。将以下依赖项添加到您的 Cargo.toml
[dependencies]
section_testing = "0.0.5"
阅读下面的示例以了解如何使用它。
示例
这里有一个示例
#[macro_use]
extern crate section_testing;
enable_sections! {
#[test]
fn example_test() {
let mut v: Vec<i32> = vec![];
fn check_123(v: &mut Vec<i32>) {
assert_eq!(*v, vec![1, 2, 3]);
if section!("reverse") {
v.reverse();
assert_eq!(*v, vec![3, 2, 1]);
}
if section!("pop+remove+insert+push") {
let three = v.pop().unwrap();
let one = v.remove(0);
v.insert(0, three);
v.push(one);
assert_eq!(*v, vec![3, 2, 1]);
}
}
if section!("push") {
v.push(1);
v.push(2);
v.push(3);
check_123(&mut v);
}
if section!("insert") {
v.insert(0, 3);
v.insert(0, 1);
v.insert(1, 2);
check_123(&mut v);
}
}
}
enable_sections!
宏修改了其中的测试函数,使它们重复运行,直到访问了所有段落。section!
宏返回一个 bool
,表示该段落是否应该在本轮迭代中运行。此示例测试将检查以下组合
push
push, reverse
push, pop+remove+insert+push
insert
insert, reverse
insert, pop+remove+insert+push
当测试失败时,封装的段落将被打印到 stderr。以下是在上面的示例中注释掉 v.push(one);
时会发生的情况
running 1 test
thread 'example_test' panicked at 'assertion failed: `(left == right)`
left: `[3, 2]`,
right: `[3, 2, 1]`', src/main.rs:30:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.
---- the failure was inside these sections ----
0) "push" at src/main.rs:34
1) "pop+remove+insert+push" at src/main.rs:25
test example_test ... FAILED
请注意,与 Rust 中的所有测试一样,段落式测试会在第一个失败时停止。这意味着你只能看到第一个失败的组合,而不是能看到所有失败的组合。如果其他组合没有先失败,上面的示例也会因为 insert, pop+remove+insert+push
的组合而失败。这是因为 Rust 内置的测试运行器没有在运行时添加新测试的 API。