喜欢的话别忘了点赞、收藏加关注哦(加关注即可阅读全文),对接下来的教程有兴趣的可以关注专栏。谢谢喵!(=^・ω・^=)
11.8.1. 忽略某些测试,执行剩余测试
某些测试执行起来非常耗时,所以在大部分情况下会想在运行cargo test
时忽略它们,除非手动运行这些测试。
对于这些测试,Rust提供了ignore
这个attribute(属性),将这些测试设为默认不执行。
看个例子:
rust
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
#[ignore]
fn expensive_test() {
assert_eq!(5, 1 + 1 + 1 + 1 + 1)
}
}
由于expensive_test
被加了ignore
这个attribute,所以在cargo test
不会执行,除非手动指定执行它们。
看一下测试结果:
$ cargo test
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.60s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 2 tests
test tests::expensive_test ... ignored
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
11.8.2. 单独执行被忽略的测试
那么如何单独运行这些被忽略的测试呢?添加参数即可,写:cargo test -- --ignored
$ cargo test -- --ignored
Compiling adder v0.1.0 (file:///projects/adder)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.61s
Running unittests src/lib.rs (target/debug/deps/adder-92948b65e88960b4)
running 1 test
test expensive_test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
Doc-tests adder
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
控制程序运行哪些测试可以确保快速返回cargo test
结果,如果时间充裕,想要执行所有的测试(包括被忽略的和没被忽略的),就写cargo test -- --include-ignored