当前位置: 首页 > news >正文

【Rust自学】11.6. 控制测试运行:并行和串行(连续执行)测试

喜欢的话别忘了点赞、收藏加关注哦,对接下来的教程有兴趣的可以关注专栏。谢谢喵!(=・ω・=)
请添加图片描述

11.6.1. 控制测试的运行方式

cargo testcargo run一样,cargo test也会编译代码并生成一个二进制文件用于测试,只不过cargo test是在测试模式下。

cargo test添加参数可以改变cargo test的行为,如果不添加任何参数,那么就会执行默认行为:

  • 并行运行所有测试
  • 在测试通过的情况下,捕获(不显示)所有输出,使读取与测试结果相关的输出更容易。如果测试不通过,输出是会显示的,以便于程序员纠错。

命令行参数分为两类:

  • 针对cargo test的参数,紧跟cargo test
  • 针对生成的可执行文件:放在--之后。例如:cargo test --help,这个参数会显示cargo test所有可用的参数。cargo test -- --help会显示所有能放在--之后的参数,也就是所有针对可执行文件的参数。

11.6.2. 并行运行测试

在运行多个测试时默认会使用多个线程来并行地运行测试,这样运行得更快,但代价是这些测试之间不能有相互依赖,而且它们不依赖于某个共享状态(环境、工作目录、环境变量…)。

如果两个测试都依赖于某个共享的状态,其中一个测试运行完时把状态改了,那么其他共享了相同状态的测试就会受到影响。

如果不想并行地运行测试,或是希望精确地控制测试时所启用的线程数量,那就可以使用--test-threads这个参数,这个参数时传递给二进制文件的。在这个参数后紧跟着线程的数量。

比如说cargo test -- --test-threads=1就是使用一个线程(单线程),这样的话如果执行多个测试它会比并行测试花费更多的时间。但它也有优点,因为它是顺序执行,所以这些测试因为共享状态而出现干扰的情况就比较少了。

11.6.3. 显式函数输出

默认,如果测试通过,Rust的test库会捕获(不显示)所有打印到标准输出的内容,比如说println!输出的内容。如果测试不通过,就会显示打印的内容和失败信息。

看个例子:

fn prints_and_returns_10(a: i32) -> i32 {println!("I got the value {a}");10
}#[cfg(test)]
mod tests {use super::*;#[test]fn this_test_will_pass() {let value = prints_and_returns_10(4);assert_eq!(value, 10);}#[test]fn this_test_will_fail() {let value = prints_and_returns_10(8);assert_eq!(value, 5);}
}
  • 在被测试的函数prints_and_returns_10调用了println!输出传入的值,然后返回10
  • 测试函数this_test_will_pass传了4给被输出函数,所以被测试函数会打印4,然后又把函数固定的返回值与10比较。这个测试会成功。
  • 测试函数this_test_will_fail传了8给被输出函数,所以被测试函数会打印8,然后又把函数固定的返回值与5比较。这个测试会失败。

测试结果:

$ cargo testCompiling silly-function v0.1.0 (file:///projects/silly-function)Finished `test` profile [unoptimized + debuginfo] target(s) in 0.58sRunning unittests src/lib.rs (target/debug/deps/silly_function-160869f38cff9166)running 2 tests
test tests::this_test_will_fail ... FAILED
test tests::this_test_will_pass ... okfailures:---- tests::this_test_will_fail stdout ----
I got the value 8
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
assertion `left == right` failedleft: 10right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtracefailures:tests::this_test_will_failtest result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00serror: test failed, to rerun pass `--lib`

测试结果中没有成功的测试所打印的句子,但是有失败的测试打印的句子:“I got the value 8”。

如果你想让成功的测试也打印句子,就可以加一个参数:cargo test -- --show-output,此时的输出如下:

$ cargo test -- --show-outputCompiling silly-function v0.1.0 (file:///projects/silly-function)Finished `test` profile [unoptimized + debuginfo] target(s) in 0.60sRunning unittests src/lib.rs (target/debug/deps/silly_function-160869f38cff9166)running 2 tests
test tests::this_test_will_fail ... FAILED
test tests::this_test_will_pass ... oksuccesses:---- tests::this_test_will_pass stdout ----
I got the value 4successes:tests::this_test_will_passfailures:---- tests::this_test_will_fail stdout ----
I got the value 8
thread 'tests::this_test_will_fail' panicked at src/lib.rs:19:9:
assertion `left == right` failedleft: 10right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtracefailures:tests::this_test_will_failtest result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00serror: test failed, to rerun pass `--lib`
http://www.lryc.cn/news/519440.html

相关文章:

  • 某漫画网站JS逆向反混淆流程分析
  • React 中事件机制详细介绍:概念与执行流程如何更好的理解
  • Day04-后端Web基础(Maven基础)
  • vue3模板语法+响应式基础
  • 【面试题】简单聊一下什么是云原生、什么是k8s、容器,容器与虚机相比优势
  • 数据挖掘实训:天气数据分析与机器学习模型构建
  • STM32如何使用内部晶振作为晶振
  • 【Maui】导航栏样式调整
  • 【黑马程序员三国疫情折线图——json+pyechart=数据可视化】
  • 如何实现多级缓存?
  • Saas数据库迁移单租户数据
  • LeetCode100之括号生成(22)--Java
  • 阿里云ios镜像源
  • 芯片:为何英伟达的GPU能在AI基础设施领域扮演重要角色?
  • Linux系统之hostname相关命令基本使用
  • Domain Adaptation(李宏毅)机器学习 2023 Spring HW11 (Boss Baseline)
  • 在php中,Fiber、Swoole、Swow这3个协程都是如何并行运行的?
  • SQLite PRAGMA
  • 使用python调用JIRA6 REST API及遇到的问题
  • 基于STM32的智能电表可视化设计:ESP8266、AT指令集、python后端Flask(代码示例)
  • 图片和短信验证码(头条项目-06)
  • 2501,wtl显示html
  • 嵌入式C语言:什么是指针?
  • 解锁 KaiwuDB 数据库工程师,开启进阶之路
  • ffmpeg7.0 aac转pcm
  • 【Pandas】pandas Series rdiv
  • 线程安全问题介绍
  • 为AI聊天工具添加一个知识系统 之27 支持边缘计算设备的资源存储库及管理器
  • 初识verilog HDL
  • VS2015 + OpenCV + OnnxRuntime-Cpp + YOLOv8 部署