Rust6.1 Writing Automated Tests

Rust学习笔记

Rust编程语言入门教程课程笔记

参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)

Lecture 11: Writing Automated Tests

src/lib.rs

//Tests: Arrange, Act, Assert
//#[test] attribute tells the compiler to compile and run the code only when running tests//cargo test --test-threads=1 //run tests in parallel or not
//cargo test -- --test-threads=1 //run tests in parallel or not//Test Organization: Unit Tests, Integration Tests
//Unit Tests: tests for one module in isolation at a time
//Integration Tests: tests for multiple modules and how they work togetherpub fn add(left: usize, right: usize) -> usize {left + right
}pub fn add_two(a: i32) -> i32 {a + 2
}#[derive(Debug)]
pub struct Rectangle {width: u32,height: u32,
}impl Rectangle {pub fn can_hold(&self, other: &Rectangle) -> bool {// self.width > other.width && self.height > other.heightself.width < other.width && self.height > other.height}
}pub fn greeting(name: &str) -> String {format!("Hello {}!", name)//String::from("Hello!")
}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 }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 }}
}fn prints_and_returns_10(a: i32) -> i32 {println!("I got the value {}", a);10
}//Test Private Functions
pub fn add_three(a: i32) -> i32 {internal_adder(a, 3)
}fn internal_adder(a: i32, b: i32) -> i32 {a + b
}//Unit Tests
#[cfg(test)]//only compile and run the following code when we run cargo test (cfg = configuration)
mod tests {use super::*; //we have added use super::*;#[test]fn it_works() {let result = add(2, 2);assert_eq!(result, 4);// assert_eq: macro that compares two values for equalityassert_ne!(result, 5);// assert_ne: macro that compares two values for inequality//If the two values are not equal, these macros will call panic and report the values }#[test]fn it_works2() -> Result<(), String> {if add(2, 2) == 4 {Ok(())} else {Err(String::from("two plus two does not equal four"))}//The Result<(), String> type is a concrete type that implements the //std::error::Error trait, which means we can use ? in the body of this test.}// #[test]// fn another() {//     panic!("Make this test fail");//panic! macro that comes with Rust// }#[test]fn larger_can_hold_smaller() {let larger = Rectangle {width: 10,height: 8,};let smaller = Rectangle {width: 5,height: 4,};// assert!(larger.can_hold(&smaller));assert!(!larger.can_hold(&smaller), "smaller can hold larger");//assert! with a custom failure message}#[test]fn greeting_contains_name() {let result = greeting("Carol");// assert!(result.contains("Carol"));assert!(result.contains("Carol"),"Greeting did not contain name, value was `{}`", result);}#[test]//#[should_panic]//#[should_panic] annotation on the test function#[should_panic(expected = "Guess value must be less than or equal to 100")]//#[should_panic] with expectedfn greater_than_100() {Guess::new(200);}#[test]fn this_test_will_pass(){let value = prints_and_returns_10(4);assert_eq!(10, value);//cargo test -- --show-output}#[test]// fn this_test_will_fail(){//     let value = prints_and_returns_10(8);//     assert_eq!(5, value);//     //cargo test will output println! messages// }#[test]fn add_two_and_two(){assert_eq!(4, add_two(2));}#[test]fn add_three_and_two(){assert_eq!(5, add_two(3));}//cargo test add#[test]fn one_hundred(){assert_eq!(102, add_two(100));}//cargo test one_hundred#[test]#[ignore]fn expensive_test(){//code that takes an hour to runassert_eq!(1+1+1+1+1+1+1+1+1+1, 10);}//cargo test -- --ignored#[test]fn internal(){assert_eq!(5, internal_adder(2, 3));//internal_adder is private}}

tests/integration_tests.rs

use adder;
mod common;#[test]
fn it_adds_two() {common::setup();assert_eq!(4, adder::add_two(2));
}//cargo test --test integration_tests

tests/another_integration_tests.rs

use adder;
mod common;
#[test]
fn it_really_adds_two() {common::setup();assert_eq!(4, adder::add_two(2));
}

tests/common/mod.rs

pub fn setup() {// setup code specific to your library's tests would go here
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/143235.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二维码在区域巡查中的应用:隐患上报、巡逻巡更、管线巡查

针对管理制度不健全、维修不及时、纸质表格容易丢失等问题&#xff0c;可以在草料上搭建区域巡查二维码系统。通过组合功能模块的方式&#xff0c;实现扫码记录巡查情况、上报隐患和整改信息、发现异常问题后及时反馈给相关负责人等功能。 比如上海延吉物业管理有限公司搭建的…

uniapp的实战总结大全

&#x1f642;博主&#xff1a;冰海恋雨 &#x1f642;文章核心&#xff1a;uniapp部分总结 目录 ​编辑 目录 前言&#xff1a; 解决方案 1. 跨平台开发 2. Vue.js生态 3. 组件库 4. 自定义组件 5. Native能力 6. 插件生态 7. 性能优化 写法 1. 模板&#xf…

在ant构建脚本中调用maven的命令

有时候想用maven管理依赖&#xff0c;用ant构建。 在ant的build.xml文件中可以使用exec这个task来调用系统命令&#xff0c;也就可以调用maven的命令。 例如&#xff0c;执行maven的命令mvn dependency:copy-dependencies&#xff0c;可以将项目的依赖提取出来&#xff0c;放…

Zookeeper 命令使用和数据说明

文章目录 一、概述二、命令使用2.1 登录 ZooKeeper2.2 ls 命令&#xff0c;查看目录树&#xff08;节点&#xff09;2.3 create 命令&#xff0c;创建节点2.4 delete 命令&#xff0c;删除节点2.5 set 命令&#xff0c;设置节点数据2.6 get 命令&#xff0c;获取节点数据 三、数…

时间序列基础->数据标签、数据分割器、数据加载器的定义和讲解(零基础入门时间序列)

一、本文介绍 各位小伙伴好&#xff0c;最近在发时间序列的实战案例中总是有一些朋友问我时间序列中的部分对数据的操作是什么含义&#xff0c;我进行了挺多的介绍和讲解但是问的人越来越多&#xff0c;所以今天在这里单独发一篇文章来单独的讲一下时间序列中对数据的处理操作…

AOMedia发布免版税沉浸音频规范IAMF

11月10日&#xff0c;开放媒体联盟&#xff08;AOMedia&#xff09;发布了旗下首个沉浸式音频规范IAMF&#xff08;https://aomediacodec.github.io/iamf/&#xff09;&#xff0c;IAMF是一种编解码器无关的容器规范&#xff0c;可以携带回放时间渲染算法和音频混音的信息&…

矩阵置零00

题目链接 矩阵置零 题目描述 注意点 使用 原地 算法 解答思路 思路是需要存储每一行以及每一列是否有0&#xff0c;因为要尽可能使用更少的空间&#xff0c;且新设置为0的格子不能对后续的判断产生影响&#xff0c;所以要在原有矩阵上存储该信息先用两个参数存储第一行和第…

ARMday06(串口)

代码&#xff1a; #include "stm32mp1xx_gpio.h" #include "stm32mp1xx_rcc.h" #include "stm32mp1xx_uart.h"void delay_ms(int ms) {int i,j;for(i0;i<ms;i){for(j0;j<2000;j);} } void init(); char getc(); void putc(char data); vo…

ISP图像处理Pipeline

参考&#xff1a;1. 键盘摄影(七)——深入理解图像信号处理器 ISP2. Understanding ISP Pipeline3. ISP图像处理流程介绍4. ISP系统综述5. ISP(图像信号处理)之——图像处理概述6. ISP 框架7. ISP(图像信号处理)算法概述、工作原理、架构、处理流程8. ISP全流程简介9. ISP流程介…

《视觉SLAM十四讲》-- 后端 1(上)

文章目录 08 后端 18.1 概述8.1.1 状态估计的概率解释8.1.2 线性系统和卡尔曼滤波&#xff08;KF&#xff09;8.1.3 非线性系统和扩展卡尔曼滤波&#xff08;EKF&#xff09;8.1.4 小结 08 后端 1 前端视觉里程计可以给出一个短时间内的轨迹和地图&#xff0c;但由于不可避免的…

GPT 写作与改编

GPT 写作与改编 文商科GPT 写作收益 改编技巧【改编一段话】【改编评价】【意识预设】落差&#xff0c;让顾客看到就感性和冲动害怕&#xff0c;让顾客看到就想买和拥有画面&#xff0c;切换空间&#xff0c;瞬间代入&#xff0c;勾人魂魄对比&#xff0c;设置参考物&#xff0…

保序回归:拯救你的校准曲线(APP)

保序回归&#xff1a;拯救你的校准曲线&#xff08;APP&#xff09; 校准曲线之所以是评价模型效能的重要指标是因为&#xff0c;校准曲线衡量模型预测概率与实际发生概率之间的一致性&#xff0c;它可以帮助我们了解模型的预测结果是否可信。一个理想的模型应该能够准确地预测…

互斥量保护资源

一、概念 在多数情况下&#xff0c;互斥型信号量和二值型信号量非常相似&#xff0c;但是从功能上二值型信号量用于同步&#xff0c; 而互斥型信号量用于资源保护。 互斥型信号量和二值型信号量还有一个最大的区别&#xff0c;互斥型信号量可以有效解决优先级反转现 象。 …

立仪科技光谱共焦在半导体领域的应用

半导体技术在近年来以极快的速度发展&#xff0c;对质量和精密度的要求也不断提升。在这样的背景下&#xff0c;用于材料与设备研究的先进检测技术如光谱共焦成像将自然地找到一席之地。下面我们将详细探讨一下光谱共焦在半导体领域中的应用。 光谱共焦技术&#xff0c;通过在细…

【Linux】进程等待

文章目录 tips一、进程等待是什么&#xff1f;二、为什么要有进程等待&#xff1f;三、怎么做到进程等待&#xff1f;先看看什么是进程等待wait和waitpidstatus参数options参数非阻塞轮询 进程等待的原理 总结 tips 下面的代码可以循环检测进程。 while :; do ps ajx | head …

长安汽车基于 Apache Doris 的车联网数据分析平台建设实践

导读&#xff1a;随着消费者更安全、更舒适、更便捷的驾驶体验需求不断增长&#xff0c;汽车智能化已成必然趋势。长安汽车智能化研究院作为长安汽车集团有限责任公司旗下的研发机构&#xff0c;专注于汽车智能化技术的创新与研究。为满足各业务部门的数据分析需求&#xff0c;…

【广州华锐互动】消防科普VR实训展馆增强群众学习兴趣和沉浸感

在现代社会&#xff0c;科技的发展已经深入到我们生活的各个角落&#xff0c;其中包括教育和信息传播领域。3D技术的引入为科普教育提供了全新的可能性。特别是在消防安全教育中&#xff0c;消防科普VR实训展馆的应用&#xff0c;不仅可以提高公众的消防安全意识&#xff0c;还…

用户画像与用户分层

用户画像是重要的数据产品和运营抓手&#xff0c;指能够描述和刻画用户信息和的数据指标。通过用户画像&#xff0c;业务经营团队可以充分、深入、准确地了解用户在不同生命周期的特征&#xff0c;来制定高效的用户经营策略。用户画像&#xff0c;不论 Persona 还是 Profile &a…

C#多线程的操作

文章目录 1 使用线程意义2 C#线程开启的四种方式2.1 异步委托开启线程2.2 通过Thread类开启线程2.3 通过线程池开启线程2.4 通过任务Task开启线程 3 前台线程和后台线程简述3.1 前台线程3.2 后台线程 4 简述Thread和Task开启线程的区别4.1 Thread效果展示4.2 Task效果展示4.3 区…

WP光电信息学院2023年网络安全季度挑战赛-测试赛

签个到就跑WP Misc MISC-没爱了&#xff0c;下一个 下载附件压缩包解压之后&#xff0c;获得一个流量包文件 使用wireShark打开流量包&#xff0c;Ctrl F 搜索flag{即可获得flag flag{Good_b0y_W3ll_Done}MISC-送你一朵小花花 下载附件压缩包解压之后&#xff0c;获得一…