乱读天书, 不求甚解
周祎骏的个人云笔记
Toggle navigation
乱读天书, 不求甚解
主页
Linux:系统配置
Linux:用户管理
Linux:优化排错
Linux:进程调度
Linux:文件系统
Linux:网络
Linux:系统服务
Linux:安全
Linux:内核
容器:Docker
容器:containerd
容器编排:Kubernetes
IAC:Terraform
大数据:Hadoop
大数据:Zookeeper
大数据:Hbase
消息队列:rsyslog
消息队列:kafka
数据库:MySQL
数据库:MongoDB
搜索引擎:Elasticsearch
时序数据库:OpenTSDB
网站服务:Nginx
编程:Bash
编程:Perl
编程:Python
编程:C
编程:JAVA
编程:Rust
版本控制:gitlab
知识管理:docusaurus
常用小工具
关于我
标签
Rust 11.00 测试
2023-02-05 10:10:59
39
0
0
admin
> cargo test # 自动化测试 * 拥有test 属性的函数是测试函数 * cargo test 命令会开始测试 * cargo test -- --test-threads=1 测试的线程数(默认多线程) * cargo test -- --nocapture 禁用输出截获(默认情况下测试成功的案例不会有输出打出来) * cargo test test_a 只测试某一个函数 * assert!() 宏调用的测试条件返回TRUE算成功,返回FALSE算失败 * assert_eq!(a,b) 两个值相等算测试通过 * assert_ne!(a,b) 两个值不相等算测试通过 ```rust #[test] fn test_a() { assert!(1 == 1); //assert!(1 == 1,"test_aaa"); //增加提示信息 } fn return_1() -> i32 {1} #[test] fn test_b() { assert_eq!(1,return_1()) //assert_eq!(1,return_1(),"test_bbb") //增加提示信息 } #[test] fn test_c() { assert_ne!(1,return_1()) //assert_ne!(1,return_1(),"test_ccc") //增加提示信息 } fn main() { println!("lala"); } ``` * should_panic 只有panic才能通过测试 ``` #[test] #[should_panic] fn test_a() { will_panic(); } #[test] #[should_panic="wawa"] 《= 只有错误信息包含指定文字才算测试通过 fn test_b() { will_panic(); } fn will_panic() { panic!("lala"); } fn main() { println!("lala"); } ``` * 带ignore 标记的方法会在测试中被忽略 * cargo test -- --ignored 会专门跑这些ignore 的函数 ``` #[test] fn test_a() { assert!(1==1); } #[test] #[ignore] fn test_b() { assert!(1==1); } fn main() { println!("lala"); } ``` # 单元测试 * 一般将单元测试与需要被测试的代码放在src 目录下的同一文件中,约定俗称再每一个源代码文件中新建一个tests 模块来放这些测试函数。 * 用#[cfg(test)] 来做标注,这个标注可以让cargo build 命令忽略它们。 # 集成测试 * 创建和src 文件夹并列的tests 文件夹,测试文件放这里。(不需要再加test标注) * cargo test --test xxx 单独运行某个特定的测试文件 * 只有library crate 才能将函数暴露给其他包调用,因此只有src/lib.rs 才能有集成测试。main.rs 不能被集成测试。
上一篇:
Rust 10.61 crypto模块
下一篇:
Terraform 0.0 基础
文档导航