集成测试 #
本文介绍 Rust 集成测试,重点说明 tests 目录、集成测试文件和共享模块。下面的示例按实际使用顺序整理,便于直接查阅和复用。
一、tests 目录 #
text
my_project/
├── Cargo.toml
├── src/
│ └── lib.rs
└── tests/
└── integration_test.rs
二、集成测试文件 #
rust
// tests/integration_test.rs
use my_project;
#[test]
fn it_adds_two() {
assert_eq!(my_project::add_two(2), 4);
}
三、共享模块 #
text
tests/
├── common/
│ └── mod.rs
└── integration_test.rs
rust
// tests/common/mod.rs
pub fn setup() {
// 测试设置
}
rust
// tests/integration_test.rs
mod common;
#[test]
fn test_with_setup() {
common::setup();
// 测试代码
}
四、总结 #
本章学习了:
- tests 目录结构
- 集成测试编写
- 共享模块使用
最后更新:2026-03-27