Rust <5>:测试
2024-10-07 16:11:34
测试运行顺序:单元测试(同处于源文件中,以 #[cfg(tests)] 标记 mod,以 #[test] 标记 function)、集成测试(位于项目根路径下的 tests 目录下,不需要 #[cfg(tests)] 标记,但依然需要 #[test] 标记 function)、文档测试。
一、选项
cargo test [testN] -- --test-threads=1 --nocapture --ignored
[testN]:可以指定单个测试模块或测试用例的完整名称单独运行,不能批量指定;但可以指定部分名称,所有名称包含此字符串的模块(包含其中所有测试用例)、测试用例均会被执行;
--test-threads=1:默认是多线程并发运行测试用例,运行速度快,但会造成输出内容交叉,指定单线程运行可保证有序输出;
--nocapture:默认测试通过的用例,其输出内容将会被捕获(屏蔽),指定此选项将会一同显示这些内容;
--ignored:被标记为 #[ignore] 的测试用例默认不会被执行,指定此项以运行这些测试用例(通常是预计耗时很长的测试);
...
二、单元测试示例
#[derive(Debug)]
pub struct Rectangle {
length: u32,
width: u32,
} impl Rectangle {
pub fn can_hold(&self, other: &Rectangle) -> bool {
return self.length > other.length && self.width > other.width;
} pub fn add_two(&mut self) {
self.length += 2;
self.width += 2;
}
} #[cfg(test)]
mod should_panic {
#[test]
fn notihing() {
println!("nothing...");
} } #[cfg(test)]
mod tests {
use super::*; #[test]
fn explotion() {
assert_eq!(2 + 2, 4);
} #[test]
fn hellokitty() {
assert_ne!(1 + 1, 4);
} #[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 smaller_cannot_hold_larger() {
let larger = Rectangle { length: 8, width: 7 };
let smaller = Rectangle { length: 5, width: 1 }; assert!(!smaller.can_hold(&larger));
} #[test]
#[ignore]
#[should_panic(expected = "t")]
fn should_panic() {
panic! ("test panic");
} #[test]
fn print_add2() {
let mut model = Rectangle { length: 8, width: 1 };
model.add_two(); assert_eq!(model.length, 10);
}
}
...
最新文章
- Android引用项目出现ClassNotFoundException
- spring bean的初始化
- java io流(字符流) 文件打开、读取文件、关闭文件
- Web前端之CSS_day1-2
- Lua.LearningLua.7-userdata
- HDU 3047
- PHP输入流php://input [转]
- [Everyday Mathematics]20150211 Carlson inequality
- ,net运行框架
- ubuntu下firefox安装Adobe Flash Player
- C#l连接OPC进行数据交互
- Oracle 创建用户并且授权
- [ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列
- HighCharts之2D金字塔图
- tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署
- prometheus+grafana 监控生产环境机器的系统信息、redis、mongodb以及jmx
- 建筑的永恒之道 (C&#183;亚历山大 著)
- 让PETSc跑得再快一些
- [ucgui] 仪表盘函数
- 2006 - MySQL server has gone away
热门文章
- 使用Excel表格的记录单功能轻松处理工作表中数据的方法
- asp.net mvc 特性路由(MapMvcAttributeRoutes)的应用
- Appium移动端自动化:元素定位uiautomatorviewer
- python 装饰器 第五步(2):带有返回值得装饰器
- Learning OSG programing---osgwindows
- 常用命令--find
- redis设置密码的问题
- [unity基础教程]Unity3D实现动态载入游戏资源(转)
- 笔记70 Spring Boot快速入门(八)(重要)
- C#驱动mysql明明数值不为空却一直说DBNull.Value的诡异情况