11.7 按测试的名称运行测试11.7.1. 按名称运行测试的子集如果想选择运行哪些测试就把测试名称一个或多个作为参数传给cargo test。看个例子pub fn add_two(a: usize) - usize { a 2 } #[cfg(test)] mod tests { use super::*; #[test] fn add_two_and_two() { let result add_two(2); assert_eq!(result, 4); } #[test] fn add_three_and_two() { let result add_two(3); assert_eq!(result, 5); } #[test] fn one_hundred() { let result add_two(100); assert_eq!(result, 102); } }如果只想运行one_hundred这个测试就写cargo test one_hundred$ cargo test one_hundred Compiling adder v0.1.0 (file:///projects/adder) Finished test profile [unoptimized debuginfo] target(s) in 0.12s Running unittests src/lib.rs (target/debug/deps/adder-302521ba8d0f0bdf) running 1 test test tests::one_hundred ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s运行单个测试时直接指定它的精确名称即可。运行多个测试时指定测试名的一部分模块名也可以作为参数所有匹配该名称的测试都会运行。举个例子假如我想运行add_two_and_two()和add_three_and_two这两个测试的名称都含有add就可以写cargo test add$ cargo test add Compiling adder v0.1.0 (file:///projects/adder) Finished test profile [unoptimized debuginfo] target(s) in 0.11s Running unittests src/lib.rs (target/debug/deps/adder-302521ba8d0f0bdf) running 2 tests test tests::add_three_and_two ... ok test tests::add_two_and_two ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s