集成测试 #

一、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