1、编写和运行测试
测试(函数)。测试:
函数
验证非测试代码的功能是否和预期一致
测试函数体(通常)执行的3个操作:
准备数据/状态
运行被测试的代码
断言(Assert)结果
解剖测试函数
测试函数需要使用test属性( attribute)进行标注
Attribute就是一段Rust代码的元数据
在函数上加# [test],可把函数变成测试函数
运行测试
使用cargo test命令运行所有测试函数
Rust会构建一个Test Runner 可执行文件
它会运行标注了test的函数,并报告其运行是否成功
当使用cargo创建 library项目的时候,会生成一个test module,里面有一个test 函数
你可以添加任意数量的test module或函数
打开cmd创建一个项目:cargo new addr --lib
进入这个文件夹:cd addr
使用vscode打开:code .
打开后在lib.rs中就能看到:
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
看使用cargo test来进行测试:
测试失败
测试函数panic就表示失败
每个测试运行在一个新线程。
当主线程看见某个测试线程挂掉了,那个测试标记为失败了。
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn orther() {
panic!("测试失败")
}
}
2、断言(Assert)
使用assert!宏检查测试结果
assert!宏,来自标准库,用来确定某个状态是否为true
true:测试通过
false:调用panic!,测试失败
impl Rectangle {
fn area(&self)->usize {
self.width * self.length
}
}
impl Rectangle {
fn can_hold(&self,other:&Rectangle)->bool {
self.width > other.width && self.length > other.length
}
// 关联函数
fn square(size:usize)->Rectangle{
Rectangle{
width:size,
length:size
}
}
}
#[derive(Debug)]
struct Rectangle {
width:usize,
length:usize
}
#[cfg(test)]
mod tests {
use super::*;
#[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));
}
#[test]
fn larger_cannot_hold_smaller() {
let larger =Rectangle {
length:8,
width:7
};
let smaller =Rectangle {
length:5,
width:1
};
assert!(!smaller.can_hold(&larger));
}
}
使用assert_eq!和 assert ne!测试相等性
都来自标准库
判断两个参数是否相等或不等
实际上,它们使用的就是==和!=运算符
断言失败:自动打印出两个参数的值
使用debug格式打印参数
要求参数实现了PartialEa和,Debug Traits (所有的基本类型和标准库里大部分类型都实现了)
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
// assert_eq!(result, 5);
assert_ne!(result, 5);
}
}
如果函数没有通过它会打印两边的值
3、自定义错误消息
添加自定义错误消息
可以向assert!、assert_eq!、assert_ne!添加可选的自定义消息
这些自定义消息和失败消息都会打印出来
assert!:第 1参数必填,自定义消息作为第 2个参数
assert_eq!和assert_ne!:前2个参数必填,自定义消息作为第3个参数。
自定义消息参数会被传递给 format!宏,可以使用占位符
pub fn greeting(name: &str) -> String {
format!("Hello {}!",name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn greetings_contain_name() {
let result = greeting("Carol");
assert!(result.contains("Carol"));
}
}
运行cargo test
上面的代码中,我们看到了错误信息,但是不够详细
现在我们来写一个自定义的信息
pub 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
);
}
}
现在我们就能够看到我们的自定义消息了,就能够很好的查询到错误的所在
4、用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 更精确
为should_panic属性添加一个可选的expected参数:
将检查失败消息中是否包含所指定的文字
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(200);
}
}
5、在测试中使用Result<T,E>
在测试中使用Result<T,E>
无需panic,可使用Result<T,E>作为返回类型编写测试:
―返回Ok:测试通过
一返回Err:测试失败
#[cfg(test)]
mod tests {
#[test]
fn it_works()->Result<(),String> {
if 2 + 3== 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
}
}
}
注意:不要在使用Result<T,E>编写的测试上标注#[should_panic]