测试(函数)
- 测试:
- 函数
- 验证非测试代码的功能是否和预期一致
测试函数体(通常)执行的 3 个操作
测试函数需要使用 test 属性(attribute) 进行标注
使用 cargo test 命令运行所有测试函数
- Rust 会构建一个 test runner 可执行文件
- 它会运行标注了 test 的函数,并报告其运行是否成功
- Rust 会构建一个 test runner 可执行文件
- 当使用 cargo 创建 library 项目的时候,会生成一个 test module,里面有一个 test 函数
- 你可以添加任意数据的 test module 或函数
创建库项目,并进入文件夹 cargo new adder --lib && cd adder 打开 lib.rs 运行 cargo test
#[cfg(test)]mod tests {#[test]fn it_works() {assert_eq!(2 + 2, 4);}}// running 1 test// test tests::it_works ... ok//// test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s// 1 个通过 0 个失败 0 个忽略 0 个性能测试 0 个被过滤的测试// Doc-tests adder// 文档测试结果,保证文档总会和实际代码同步// running 0 tests//// test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
测试失败
- 测试函数 panic 就表示失败
- 每个测试运行在一个新线程
当主线程看见某个测试线程挂掉了,那个测试标记为失败了 ```rust
[cfg(test)]
mod tests {
[test]
fn exploration() {
assert_eq!(2 + 2, 4);
}
[test]
fn another() {
panic!("Make this test fail")
} }
// running 2 tests
// test tests::exploration … ok
// test tests::another … FAILED
//
// failures:
//
// —— tests::another stdout ——
// thread ‘tests::another’ panicked at ‘Make this test fail’, src/lib.rs:10:9
// note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
//
//
// failures:
// tests::another
//
// test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
//
// error: test failed, to rerun pass ‘—lib’
<a name="DNcUS"></a># 断言(Assert)<a name="FDRSc"></a>## 使用 assert! 宏检查测试结果- assert! 宏,来自标准库,用来确定某个状态是否为 true- true:测试通过- false:调用 panic,测试失败```rust#[derive(Debug)]pub struct Rectangle {length: u32,width: u32,}impl Rectangle {pub fn can_hold(&self, other: &Rectangle) -> bool {self.length > other.length && self.width > other.width}}#[cfg(test)]mod tests {// test 模块与其他模块没有任何区别use super::*; // 将外部模块所以内容导入到 test 模块#[test]fn larger_can_hold_smaller() {// 准备数据let larger = Rectangle {length: 8,width: 7,};let smaller = Rectangle {length: 5,width: 1,};// 执行待测试代码assert!(larger.can_hold(&smaller));}}
使用 assert_eq! 和 assert_ne! 测试相等性
- 都来自标准库
- 判断两个参数是否 相等 或 不等
- 实际上,它们使用的就是 == 和 != 运算符
- 断言失败:自动打印出两个参数的值
- 使用 debug 格式打印参数
- 要求参数实现了 PartialEq 和 Debug Traits(所有的基本类型和标准库里大部分类型都实现了) ```rust pub fn add_two(a: i32) -> i32 { a + 2 }
[cfg(test)]
mod tests { use super::*;
#[test]fn if_adds_two() {assert_eq!(4, add_two(2)); // 期待的值和执行结果位置不要求}
}
<a name="fTTR0"></a># 自定义错误消息<a name="yxXDd"></a>## 添加自定义错误消息- 可以向 assert!、assert_eq!、assert_ne! 添加可选的自定义消息- 这些自定义消息和失败消息都会打印出来- assert!: 第 1 参数必填,自定义消息作为第 2 个参数- assert_eq! 和 assert_ne!: 前 2 个参数必填,自定义消息作为第3个参数- 自定义消息参数会被传递给 format! 宏,可以使用 {} 占位符```rustpub fn greeting(name: &str) -> String {format!("Hello")}#[cfg(test)]mod tests {use super::*;#[test]fn greetings_contain_name() {let result = greeting("Carol");assert!(result.contains("Carol"),"Greeting didn't contain name, value was '{}'",result);}}
用 should_panic 检查恐慌
验证错误处理的情况
- 测试除了验证代码的返回值是否正确,还需验证代码是否如预期的处理了发生错误的情况
- 可验证代码在特定情况下是否发生了 panic
should_panic 属性 (attribute):
- 函数 panic:测试通过
函数没有 panic:测试失败
pub struct Guess {value: u32,}impl Guess {pub fn new(value: u32) -> Guess {if value < 1 || value > 100 {panic!("Guess value must be between 1 and 100, got {}.", value)}Guess { value }}}#[cfg(test)]mod tests {use super::*;#[test]#[should_panic]fn greater_than_100() {Guess::new(200);}}
让 should_panic 更精确
```rust pub struct Guess { value: u32, } impl Guess { pub fn new(value: u32) -> Guess { if value < 1 {
panic!("Guess value must be greater than or equal to 1, got {}.",value)
} else if value > 100 {
panic!("Guess value must be less than or equal to 100, got {}.",value)
} Guess { value } } }
[cfg(test)]
mod tests { use super::*;
[test]
[should_panic(expected = “Guess value must be less than or equal to 100”)]
fn greater_than_100() { Guess::new(0); } }
// note: panic did not contain expected string
// panic message: "Guess value must be greater than or equal to 1, got 0.",
// expected substring: "Guess value must be less than or equal to 100"
<a name="GkGd0"></a># 在测试中使用 Result<T, E>- 无需 panic,可使用 Result<T, E> 作为返回类型编写测试:- 返回 Ok:测试通过- 返回 Err:测试失败```rust#[cfg(test)]mod tests {#[test]fn it_workd() -> Result<(), String> {if 2 + 3 == 4 {Ok(())} else {Err(String::from("two plus two does not equal four"))}}}
- 注意:不要在使用 Result
编写的测试上标注 #[should_panic] 因为在运行失败时会直接返回 Err 不会发生 panic
