Rust测试框架库testing的使用,高效单元测试与集成测试工具库
Rust测试框架库testing的使用,高效单元测试与集成测试工具库
安装 运行以下Cargo命令在您的项目目录中: cargo add testing
或者将以下行添加到您的Cargo.toml: testing = “15.0.0”
元数据 包标识符:pkg:cargo/testing@15.0.0 发布时间:约1个月前 版本:2021 edition 许可证:Apache-2.0 大小:16.6 KiB
所有者 Donny/강동윤 SWC Bot
完整示例demo:
// 单元测试示例
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_addition() {
// 测试加法运算
assert_eq!(2 + 2, 4);
}
#[test]
fn test_string_concatenation() {
// 测试字符串拼接
let s1 = "Hello";
let s2 = " World";
assert_eq!(format!("{}{}", s1, s2), "Hello World");
}
#[test]
#[should_panic]
fn test_panic() {
// 测试应该panic的情况
panic!("This test should panic");
}
}
// 集成测试示例
// tests/integration_test.rs
#[cfg(test)]
mod integration_tests {
use testing;
#[test]
fn test_library_function() {
// 测试库函数
assert!(testing::some_function());
}
}
// 异步测试示例
#[cfg(test)]
mod async_tests {
use tokio::test;
#[test]
async fn test_async_function() {
// 测试异步函数
let result = some_async_function().await;
assert!(result.is_ok());
}
}
// 基准测试示例
#[cfg(test)]
mod bench_tests {
use test::Bencher;
#[bench]
fn bench_function(b: &mut Bencher) {
// 基准测试
b.iter(|| {
// 要测试性能的代码
});
}
}
1 回复
Rust测试框架库testing的使用指南
介绍
Rust内置的测试框架testing是一个功能强大的测试工具,无需额外依赖即可进行单元测试和集成测试。它提供了简洁的语法和丰富的断言功能,帮助开发者编写高质量的测试代码。
使用方法
1. 基本测试结构
#[cfg(test)]
mod tests {
#[test]
fn test_addition() {
assert_eq!(2 + 2, 4);
}
#[test]
#[should_panic]
fn test_panic() {
panic!("This test should panic");
}
}
2. 常用断言宏
#[test]
fn test_assertions() {
// 相等断言
assert_eq!(5, 5);
// 不等断言
assert_ne!(3, 4);
// 布尔断言
assert!(true);
// 包含错误信息的断言
assert!(2 > 1, "Two should be greater than one");
}
3. 测试组织
// 单元测试(通常放在与被测代码相同的文件中)
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}
#[cfg(test)]
mod tests {
use super::math::add;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
}
4. 集成测试
在项目根目录创建tests
文件夹:
project/
├── src/
│ └── lib.rs
└── tests/
└── integration_test.rs
// tests/integration_test.rs
use my_crate;
#[test]
fn integration_test() {
assert_eq!(my_crate::add(10, 20), 30);
}
5. 测试运行命令
# 运行所有测试
cargo test
# 运行特定测试
cargo test test_add
# 显示测试输出(即使测试通过)
cargo test -- --nocapture
# 运行集成测试
cargo test --test integration_test
6. 测试配置
#[test]
#[ignore] // 忽略此测试
fn expensive_test() {
// 耗时操作
}
#[test]
#[should_panic(expected = "value must be positive")] // 预期panic信息
fn test_negative_input() {
// 测试代码
}
完整示例代码
// src/lib.rs
// 数学运算模块
pub mod math {
/// 加法函数
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
/// 除法函数,除数为0时panic
pub fn divide(a: i32, b: i32) -> i32 {
if b == 0 {
panic!("value must be positive");
}
a / b
}
}
// 单元测试模块
#[cfg(test)]
mod tests {
use super::math::{add, divide};
// 基本加法测试
#[test]
fn test_addition() {
assert_eq!(add(2, 2), 4);
}
// 测试panic情况
#[test]
#[should_panic]
fn test_panic() {
panic!("This test should panic");
}
// 断言宏使用示例
#[test]
fn test_assertions() {
// 相等断言
assert_eq!(5, 5);
// 不等断言
assert_ne!(3, 4);
// 布尔断言
assert!(true);
// 包含错误信息的断言
assert!(2 > 1, "Two should be greater than one");
}
// 测试数学加法函数
#[test]
fn test_math_add() {
assert_eq!(add(2, 3), 5);
assert_eq!(add(-1, 1), 0);
assert_eq!(add(0, 0), 0);
}
// 被忽略的耗时测试
#[test]
#[ignore]
fn expensive_test() {
// 模拟耗时操作
std::thread::sleep(std::time::Duration::from_secs(10));
assert!(true);
}
// 测试预期的panic信息
#[test]
#[should_panic(expected = "value must be positive")]
fn test_divide_by_zero() {
divide(10, 0);
}
}
// tests/integration_test.rs
// 集成测试示例
use testing_demo::math;
#[test]
fn integration_test_add() {
assert_eq!(math::add(10, 20), 30);
assert_eq!(math::add(100, 200), 300);
}
#[test]
fn integration_test_complex_operations() {
let result = math::add(math::add(5, 3), math::add(2, 4));
assert_eq!(result, 14);
}
最佳实践
- 为每个功能模块编写对应的测试
- 使用有意义的测试函数名
- 保持测试的独立性和可重复性
- 合理使用
#[ignore]
标记耗时测试 - 编写清晰的测试失败信息
通过合理使用Rust的testing框架,可以显著提高代码质量和开发效率。