Rust Course学习(编写测试)

news/2024/9/22 21:05:44/

如果友友你的计算机上没有安装Rust,可以直接安装:Rust 程序设计语言 (rust-lang.org)icon-default.png?t=N7T8https://www.rust-lang.org/zh-CN/

Introduce 介绍

       Testing in Rust involves writing code specifically designed to verify that other code works as expected. It’s important because it helps ensure the reliability, correctness, and robustness of Rustprograms by detecting bugs, regressions, and edge cases early in the development process.
      Rust中的测试涉及编写专门设计的代码,以验证其他代码是否按预期工作。它很重要,因为它有助于通过在开发过程的早期检测错误,回归和边缘情况来确保Rust程序的可靠性,正确性和健壮性。

     To start experimenting with tests, we need to create a library crate, we can do that by running the following command.
     要开始测试,我们需要创建一个库crate,我们可以通过运行以下命令来完成。

cargo new testing_in_rust --libv

       Creating a new library crate 
       创建一个新的库crate,可以参考小北之前的main.rs 文件。

      In the lib.rs file we already have a pre-written test, let’s break it down to understand it better.
      在 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);}
}
  • The #[cfg(test)] attribute indicates that the module contains test code, which will only be compiled when running tests.
  • #[cfg(test)] 属性表示模块包含测试代码,只有在运行测试时才会编译这些代码。
  • mod tests { ... } defines a module named tests specifically for organizing test code.
  • mod tests { ... } 定义了一个名为 tests 的模块,专门用于组织测试代码。
  • use super::*; imports items from the parent module (super) into the test module's scope, allowing access to the add function.
  • use super::*; 将项目从父模块( super )导入到测试模块的作用域中,允许访问 add 函数。
  • #[test] attribute marks a function as a test case. In this case, fn it_works() { ... } is the test case.
  • #[test] 属性将函数标记为测试用例。在本例中, fn it_works() { ... } 是测试用例。
  • Inside it_works(), let result = add(2, 2); calls the add function with arguments 2 and 2, storing the result in result.
  • 在 it_works() 中, let result = add(2, 2); 使用参数 2 和 2 调用 add 函数,并将结果存储在 result 中。
  • assert_eq!(result, 4); verifies that result is equal to 4. If the assertion fails, the test case fails, indicating that the add function did not produce the expected result.
  • assert_eq!(result, 4); 验证 result 等于 4 。如果断言失败,则测试用例失败,表明 add 函数没有产生预期的结果。
  • Running this test will verify that the add function correctly adds two usize values and returns the expected result, 4.
  • 运行此测试将验证 add 函数是否正确地将两个 usize 值相加并返回预期结果 4
  • To run the tests we can use the cargo test command
  • 要运行测试,我们可以使用 cargo test 命令
    cargo test 


http://www.ppmy.cn/news/1455356.html

相关文章

【QEMU系统分析之实例篇(十八)】

系列文章目录 第十八章 QEMU系统仿真的机器创建分析实例 文章目录 系列文章目录第十八章 QEMU系统仿真的机器创建分析实例 前言一、QEMU是什么?二、QEMU系统仿真的机器创建分析实例1.系统仿真的命令行参数2.创建后期后端驱动qemu_create_late_backends()qtest_serv…

Llama3-Tutorial之LMDeploy高效部署Llama3实践

Llama3-Tutorial之LMDeploy高效部署Llama3实践 Llama 3 近期重磅发布,发布了 8B 和 70B 参数量的模型,lmdeploy团队对 Llama 3 部署进行了光速支持!!! 书生浦语和机智流社区同学光速投稿了 LMDeploy 高效量化部署 Llam…

茅台葡萄酒打出节日新式营销“组合拳”,两月内落地品鉴会超千桌

执笔 | 尼 奥 编辑 | 古利特 2024年1-3月酒类进出口数据显示,葡萄酒进口量微增3.66%,进口额同比下滑11%,一季度整体跌势大缓,逐步走出普遍低迷的行情。与之相反的是,作为国产葡萄酒代表的茅台葡萄酒继续保持向上的战…

算法系列--BFS解决拓扑排序

💕"请努力活下去"💕 作者:Lvzi 文章主要内容:算法系列–算法系列–BFS解决拓扑排序 大家好,今天为大家带来的是算法系列--BFS解决拓扑排序 前言:什么是拓扑排序 拓扑排序–解决有顺序的排序问题(要做事情的先后顺序) …

《深入理解kafka-核心设计与实践原理》第三章:消费者

第三章:消费者 3.1 消费者与消费组 3.1.1 消费者(Consumer) 3.1.2 消费组(Consumer Group) 3.1.3 消息投递模式 3.2 客户端开发 3.2.1 必要的配置参数 3.2.2 订阅主题与分区 3.2.3 反序列化 3.2.4 消费消息 3.2.5 位移提交 3.2.5.1 offset 3.2.5.2 消费后的提交方式…

ANSYS许可分析方法

在工程设计与仿真领域,ANSYS软件凭借其强大的功能和卓越的性能,已成为了行业内的领导者。然而,随着业务的不断拓展和软件版本的升级,如何有效地分析和利用ANSYS许可证,确保仿真工作的顺利进行,已成为企业关…

JavaScript 常见的40个保留字大全!

你好,我是云桃桃。 一个希望帮助更多朋友快速入门 WEB 前端的程序媛。 云桃桃 大专生,一枚程序媛,感谢关注。回复 “前端基础题”,可免费获得前端基础 100 题汇总,回复 “前端基础路线”,可获取完整web基础…

一款开源的原神工具箱,专为现代化 Windows 平台设计,旨在改善桌面端玩家的游戏体验

Snap.Hutao 胡桃工具箱是一款以 MIT 协议开源的原神工具箱,专为现代化 Windows 平台设计,旨在改善桌面端玩家的游戏体验。通过将既有的官方资源与开发团队设计的全新功能相结合,提供了一套完整且实用的工具集,且无需依赖任何移动设…