diff --git a/src/ch11-02-running-tests.md b/src/ch11-02-running-tests.md index 11b36ef..8da6c5e 100644 --- a/src/ch11-02-running-tests.md +++ b/src/ch11-02-running-tests.md @@ -54,57 +54,24 @@ mod tests { 运行 `cargo test` 将会看到这些测试的输出: -```text -running 2 tests -test tests::this_test_will_pass ... ok -test tests::this_test_will_fail ... FAILED - -failures: - ----- tests::this_test_will_fail stdout ---- -I got the value 8 -thread 'tests::this_test_will_fail' panicked at 'assertion failed: `(left == right)` - left: `5`, - right: `10`', src/lib.rs:19:9 -note: Run with `RUST_BACKTRACE=1` for a backtrace. - -failures: - tests::this_test_will_fail - -test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out +```console +{{#include ../listings/ch11-writing-automated-tests/listing-11-10/output.txt}} ``` -注意输出中不会出现测试通过时打印的内容,即 `I got the value 4`。因为当测试通过时,这些输出会被截获。失败测试的输出 `I got the value 8` ,则出现在输出的测试摘要部分,同时也显示了测试失败的原因。 +注意输出中不会出现测试通过时打印的内容,即 `I got the value 4`。因为当测试通过时,这些输出会被截获。失败测试的输出 `I got the value 8`,则出现在输出的测试摘要部分,同时也显示了测试失败的原因。 -如果你希望也能看到通过的测试中打印的值,截获输出的行为可以通过 `--nocapture` 参数来禁用: +如果你希望也能看到通过的测试中打印的值,可以通过在末尾增加 `--show-output` 参数来告知 Rust 显示通过测试的输出: -```text -$ cargo test -- --nocapture +```console +$ cargo test -- --show-output ``` -使用 `--nocapture` 参数再次运行示例 11-10 中的测试会显示如下输出: +使用 `--show-output` 参数再次运行示例 11-10 中的测试会显示如下输出: -```text -running 2 tests -I got the value 4 -I got the value 8 -test tests::this_test_will_pass ... ok -thread 'tests::this_test_will_fail' panicked at 'assertion failed: `(left == right)` - left: `5`, - right: `10`', src/lib.rs:19:9 -note: Run with `RUST_BACKTRACE=1` for a backtrace. -test tests::this_test_will_fail ... FAILED - -failures: - -failures: - tests::this_test_will_fail - -test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out +```console +{{#include ../listings/ch11-writing-automated-tests/output-only-01-show-output/output.txt}} ``` -注意测试的输出和测试结果的输出是相互交叉的,这是由于测试是并行运行的(见上一部分)。尝试一同使用 `--test-threads=1` 和 `--nocapture` 功能来看看输出是什么样子! - ### 通过指定名字来运行部分测试 有时运行整个测试集会耗费很长时间。如果你负责特定位置的代码,你可能会希望只运行与这些代码相关的测试。你可以向 `cargo test` 传递所希望运行的测试名称的参数来选择运行哪些测试。