Rust 内置测试:无需额外框架
对比:单元测试
JS
TypeScript(Jest)// package.json: "jest": "^29.0.0"
import { add } from './math';
describe('add', () => {
it('两正数相加', () => {
expect(add(2, 3)).toBe(5);
});
it('负数', () => {
expect(add(-1, -2)).toBe(-3);
});
});Rs
Rust(内置)// 直接在源文件里写,不需要任何包
pub fn add(a: i32, b: i32) -> i32 { a + b }
#[cfg(test)] // 只在测试时编译
mod tests {
use super::*;
#[test]
fn 两正数相加() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn 负数() {
assert_eq!(add(-1, -2), -3);
}
}→
Rust 测试是语言的一部分,不是第三方库。cargo test 自动发现和运行所有 #[test] 函数,包括文档里的代码示例。
异步测试
#[tokio::test]
async fn test_api_endpoint() {
let app = create_test_app().await;
let response = app
.oneshot(Request::builder()
.uri("/api/posts")
.body(Body::empty())
.unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}SQLx 测试:自动回滚
#[sqlx::test]
async fn test_create_user(pool: PgPool) {
// pool 是测试专用连接,测试结束后自动回滚
let user = create_user(&pool, "test@example.com", "testuser", "hash")
.await
.unwrap();
assert_eq!(user.email, "test@example.com");
assert!(user.id.to_string().len() > 0);
}
// 无论测试成功还是失败,数据库状态自动恢复文档测试:让示例永远正确
/// 计算两数之和。
///
/// # 示例
///
/// ```
/// let result = my_crate::add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}cargo test 会运行文档里的代码块,保证示例永远是最新且正确的。
开发工具链
# 代码质量检查(比编译器更严格)
cargo clippy -- -D warnings # 警告当错误
# 统一代码格式
cargo fmt
cargo fmt --check # 只检查,用于 CI
# 生成并查看文档
cargo doc --open
# 安全漏洞扫描
cargo audit
# 文件变化时自动运行
cargo watch -x test
cargo watch -x 'clippy -- -D warnings'在 CI 里运行:cargo fmt --check && cargo clippy -- -D warnings && cargo test,三行命令保证代码质量。
实战项目:完整测试套件
实战项目
为博客 API 添加完整测试套件
90 分钟中级
为第 9-10 章的博客 API 添加:单元测试(认证逻辑)、集成测试(API 端点)、SQLx 数据库测试(CRUD 操作),配置 GitHub Actions CI。
#[tokio::test] 异步测试#[sqlx::test] 数据库测试Axum 测试客户端 oneshotCI 配置
cd blog-api && cargo test实战视频
Rust 测试体系与工具链
0:00 / 0:00CC
单元测试、异步测试、SQLx 自动回滚、文档测试、clippy 和 fmt 的 CI 集成。