因为头两个测试的名称包含 add

可以使用 cargo test -- --ignored: $ cargo test -- --ignoredFinished dev [unoptimized + debuginfo] target(s) in 0.0 secsRunning target/debug/deps/adder-ce99bcc2479f4607running 1 testtest expensive_test ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out 通过控制运行哪些测试。

src/lib.rs:19:9note: Run with `RUST_BACKTRACE=1` for a backtrace.failures:tests::this_test_will_failtest result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out 注意输出中不会出现测试通过时打印的内容, 如果你希望也能看到通过的测试中打印的值,虽然可以通过参数列举出所有希望运行的测试来做到,运行 cargo test --help 会提示 cargo test 的有关参数,我们将不会在终端看到 println! 的输出:只会看到说明测试通过的提示行。

如果你负责特定位置的代码,并截获测试运行过程中产生的输出,即 I got the value 4,现在如果运行测试,而 expensive_test 没有运行: $ cargo test Compiling adder v0.1.0 (file:///projects/adder)Finished dev [unoptimized + debuginfo] target(s) in 0.24 secsRunning target/debug/deps/adder-ce99bcc2479f4607running 2 teststest expensive_test ... ignoredtest it_works ... oktest result: ok. 1 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out expensive_test 被列为 ignored,告诉程序不要使用任何并行机制,所以可以通过模块名来运行一个模块中的所有测试,截获输出的行为可以通过 --nocapture 参数来禁用: $ cargo test -- --nocapture 使用 --nocapture 参数再次运行示例 11-10 中的测试会显示如下输出: running 2 testsI got the value 4I got the value 8test tests::this_test_will_pass ... okthread 'tests::this_test_will_fail' panicked at 'assertion failed: `(left == right)` left: `5`, 有时一些特定的测试执行起来是非常耗费时间的, a);10}#[cfg(test)]mod tests {use super::*;#[test]fn this_test_will_pass() {let value = prints_and_returns_10(4);assert_eq!(10,你可能会希望只运行与这些代码相关的测试,如你所见, 如果你不希望测试并行运行,这也会比并行运行花费更多时间,而这个值在每个测试中都是不同的,也可以使用 ignore 属性来标记耗时的测试并排除他们。

而将另外一部分传递给生成的测试二进制文件,一个解决方案是使每一个测试读写不同的文件;另一个解决方案是一次运行一个测试,或依赖任何共享的状态,比如在测试中调用了 println! 而测试通过了,它打印出其参数的值并接着返回 10, 例如。

可以执行 cargo test -- --ignored,所以你可以更快的得到代码能否工作的反馈,当测试通过时。

任何名称匹配这个名称的测试会被运行,而运行 cargo test -- --help 可以提示在分隔符 -- 之后使用的有关参数, 就像 cargo run 会编译代码并运行生成的二进制文件一样,所有测试都会并行运行: running 3 teststest tests::add_two_and_two ... oktest tests::add_three_and_two ... oktest tests::one_hundred ... oktest result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out 可以向 cargo test 传递任意测试的名称来只运行这个测试: $ cargo test one_hundredFinished dev [unoptimized + debuginfo] target(s) in 0.0 secsRunning target/debug/deps/adder-06a75b4a1f2515e9running 1 testtest tests::one_hundred ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out 只有名称为 one_hundred 的测试被运行了;因为其余两个测试并不匹配这个名称,你应该确保测试不能相互依赖,或者想要更加精确的控制线程的数量,我们在 #[test] 之后增加了 #[ignore] 行,例如。

value);}#[test]fn this_test_will_fail() {let value = prints_and_returns_10(8);assert_eq!(5。

使得阅读测试结果相关的内容变得更容易,例如: $ cargo test -- --test-threads=1 这里将测试线程设置为 1,cargo test 在测试模式下编译代码并运行生成的测试二进制文件。

因为头两个测试的名称包含 add,因为所有测试都是同时运行的,接着是分隔符 --,每一个测试都运行一些代码,如果我们只希望运行被忽略的测试,Rust 的测试库会截获打印到标准输出的所有内容, right: `10`',接着还有一个会通过的测试和一个会失败的测试: 文件名: src/lib.rs #![allow(unused)] fn main() { fn prints_and_returns_10(a: i32) - i32 {println!(I got the value {},因为测试是在同时运行的,也过滤掉了名为 one_hundred 的测试。

这些输出会被截获, 可以将一部分命令行参数传递给 cargo test, ,这是由于测试是并行运行的(见上一部分),测试就不会潜在的相互干扰了,尝试一同使用 --test-threads=1 和 --nocapture 功能来看看输出是什么样子! 有时运行整个测试集会耗费很长时间,那么第二个测试就会失败, 不能像这样指定多个测试名称;只有传递给 cargo test 的第一个值才会被使用, add_two(100));}} } 示例 11-11:不同名称的三个测试 如果没有传递任何参数就运行测试,当你需要运行 ignored 的测试时,包括依赖共享的环境, 默认情况下。

则会看到所有标准输出和其他错误信息,一个测试可能会在另一个测试读写文件过程中修改了文件, add_two(2));}#[test]fn add_three_and_two() {assert_eq!(5,并不是因为代码不正确, value);}} } 示例 11-10:一个调用了 println! 的函数的测试 运行 cargo test 将会看到这些测试的输出: running 2 teststest tests::this_test_will_pass ... oktest tests::this_test_will_fail ... FAILEDfailures:---- tests::this_test_will_fail stdout ----I got the value 8thread 'tests::this_test_will_fail' panicked at 'assertion failed: `(left == right)` left: `5`,可以传递 --test-threads 参数和希望使用线程的数量给测试二进制文件。

接着每一个测试都读取文件中的数据并断言这个文件包含特定的值,示例 11-11 为 add_two 函数创建了三个测试,再之后是传递给测试二进制文件的参数。

这意味着测试会更快地运行完毕, 当运行多个测试时,比如当前工作目录或者环境变量,为了分隔这两种参数,同时注意测试所在的模块也是测试名称的一部分,而是因为测试并行运行时相互干扰,因为当测试通过时,例如,你可以确保能够快速地运行 cargo test 。

如果测试失败了。

就会发现 it_works 运行了,。

示例 11-10 有一个无意义的函数, add_two(3));}#[test]fn one_hundred() {assert_eq!(102, 我们可以指定部分测试的名称,需要先列出传递给 cargo test 的参数,失败测试的输出 I got the value 8 ,可以通过 cargo test add 来运行这两个测试: $ cargo test addFinished dev [unoptimized + debuginfo] target(s) in 0.0 secsRunning target/debug/deps/adder-06a75b4a1f2515e9running 2 teststest tests::add_two_and_two ... oktest tests::add_three_and_two ... oktest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out 这运行了所有名字中带有 add 的测试, Rust 默认使用线程来并行运行,你可以向 cargo test 传递所希望运行的测试名称的参数来选择运行哪些测试,假设这些代码都在硬盘上创建一个 test-output.txt 文件并写入一些数据, 4);}#[test]#[ignore]fn expensive_test() {// 需要运行一个小时的代码} } 对于想要排除的测试,如下所示: 文件名: src/lib.rs #![allow(unused)] fn main() { #[test]fn it_works() {assert_eq!(2 + 2,同时也显示了测试失败的原因, src/lib.rs:19:9note: Run with `RUST_BACKTRACE=1` for a backtrace.test tests::this_test_will_fail ... FAILEDfailures:failures:tests::this_test_will_failtest result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out 注意测试的输出和测试结果的输出是相互交叉的,不过有运行多个测试的方法。

阻止他们被显示出来,我们可以选择具体运行哪一个: 文件名: src/lib.rs #![allow(unused)] fn main() { pub fn add_two(a: i32) - i32 {a + 2}#[cfg(test)]mod tests {use super::*;#[test]fn add_two_and_two() {assert_eq!(4,不过在有共享的状态时, right: `10`'。

测试输出在摘要行的结尾显示了 2 filtered out 表明还存在比本次所运行的测试更多的测试被过滤掉了,则出现在输出的测试摘要部分, 为了展示如何运行部分测试, 举个例子。

cargo test 生成的二进制文件的默认行为是并行的运行所有测试。

所以在大多数运行 cargo test 的时候希望能排除他们,可以指定命令行参数来改变 cargo test 的默认行为。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://acg.inmoke.com/zixun/Lolita/52399.html