Rust Course学习(编写测试)

如果友友你的计算机上没有安装Rust ,可以直接安装:Rust 程序设计语言 (rust-lang.org)https://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 Rust programs 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 文件中,我们已经有了一个预先编写的测试,让我们分解它以更好地理解它。

rust 复制代码
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 命令

    rust 复制代码
    cargo test 
相关推荐
WHYBIGDATA11 分钟前
Scala中高级的函数编程
开发语言·后端·scala
知识分享小能手17 分钟前
从新手到高手:Scala函数式编程完全指南,Scala 访问修饰符(6)
大数据·开发语言·后端·python·数据分析·scala·函数式编程
点云侠27 分钟前
matlab 干涉图仿真
开发语言·人工智能·算法·计算机视觉·matlab
是Yu欸28 分钟前
【前端实现】在父组件中调用公共子组件:注意事项&逻辑示例 + 将后端数组数据格式转换为前端对象数组形式 + 增加和删除行
前端·vue.js·笔记·ui·vue
2401_8584255533 分钟前
Batch文件中使用tasklist命令:深入掌握进程监控与分析
开发语言·batch
Eiceblue33 分钟前
用Python轻松转换Markdown文件为PDF文档
开发语言·vscode·python·pdf·word
杰哥在此34 分钟前
Java面试题:解释跨站脚本攻击(XSS)的原理,并讨论如何防范
java·开发语言·面试·编程·xss
youyoufenglai35 分钟前
【HarmonyOS4学习笔记】《HarmonyOS4+NEXT星河版入门到企业级实战教程》课程学习笔记(二十)
笔记·学习·鸿蒙
nice肥牛39 分钟前
Python爬取国家医保平台公开数据
开发语言·爬虫·python·国家医保平台
hss27991 小时前
【Qt】QTableWidget设置可以选择多行多列,并能复制选择的内容到剪贴板
开发语言·qt