mirror of
https://github.com/RustyCab/LearnRustEasy.git
synced 2026-02-04 02:33:24 +08:00
update
This commit is contained in:
@@ -41,12 +41,18 @@
|
||||
- [引用循环、内存泄露、Weak智能指针](./chapter_3/chapter_3_16_7.md)
|
||||
- [包、crate、模块](./chapter_3/chapter_3_17.md)
|
||||
- [包、crate和模块介绍](./chapter_3/chapter_3_17_1.md)
|
||||
- [测试-todo](./chapter_3/chapter_3_18.md)
|
||||
- [再谈注释-todo](./chapter_3/chapter_3_19.md)
|
||||
- [测试](./chapter_3/chapter_3_18.md)
|
||||
- [编写测试](./chapter_3/chapter_3_18_1.md)
|
||||
- [运行测试](./chapter_3/chapter_3_18_2.md)
|
||||
- [再谈注释](./chapter_3/chapter_3_19.md)
|
||||
- [Rust并发编程](./chapter_3/chapter_3_20.md)
|
||||
- [unsafe编程](./chapter_3/chapter_3_21.md)
|
||||
- [FFI介绍-todo](./chapter_3/chapter_3_22.md)
|
||||
- [宏介绍-todo](./chapter_3/chapter_3_23.md)
|
||||
- [FFI介绍](./chapter_3/chapter_3_22.md)
|
||||
- [在Rust中调用C](./chapter_3/chapter_3_22_1.md)
|
||||
- [在C中调用Rust](./chapter_3/chapter_3_22_2.md)
|
||||
- [宏介绍](./chapter_3/chapter_3_23.md)
|
||||
- [声明宏](./chapter_3/chapter_3_23_1.md)
|
||||
- [过程宏](./chapter_3/chapter_3_23_2.md)
|
||||
- [Rust使用技巧]()
|
||||
- [Rust代码风格与格式化](./chapter_4/chapter_4_1.md)
|
||||
- [使用 Clippy 进行代码静态检查](./chapter_4/chapter_4_2.md)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# 3.18 测试
|
||||
|
||||
- [3.18.1 编写测试](./chapter_3_18_1.md)
|
||||
- [3.18.2 运行测试](./chapter_3_18_2.md)
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
# 3.18.2运行测试
|
||||
# 3.18.2 运行测试
|
||||
|
||||
上一节演示了如何编写测试,对于运行测试则只提到了简单的命令cargo test。本节对执行测试进行详细介绍。
|
||||
|
||||
## 1. 并行或连续运行测试
|
||||
|
||||
当运行多个测试时,Rust默认使用线程并行运行。如果不希望测试并行运行,或者更加精确的控制线程的数量,可以传递 --test-threads参数来控制,示例如下:
|
||||
```
|
||||
cargo test -- --test-threads=1 # 使用一个线程运行测试
|
||||
```
|
||||
|
||||
## 2. 显示测试中的打印
|
||||
|
||||
有的时候,需要在运行测试时打印内容到标准输出,可以添加-- --nocapture或者-- --show-output,例如有如下代码:
|
||||
```Rust
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
@@ -35,6 +39,7 @@ cargo test -- --show-output
|
||||
```
|
||||
|
||||
## 3. 运行单个测试
|
||||
|
||||
运行测试时,可以通过指定测试函数的名字来运行某个特定的测试函数。例如有如下测试代码:
|
||||
```Rust
|
||||
pub fn add_two(a: i32) -> i32 {
|
||||
@@ -71,6 +76,7 @@ cargo test add_two_and_two
|
||||

|
||||
|
||||
## 4. 过滤运行测试
|
||||
|
||||
还可以指定部分测试的名称,任何名称匹配这个名称的测试会被运行。例如,因为上面的代码中,前两个测试的名称包含 add,则可以通过 cargo test add 来运行这两个测试:
|
||||
```
|
||||
cargo test add
|
||||
|
||||
Reference in New Issue
Block a user