<font style="color:rgb(13, 13, 13);">crate</font> 的公共 <font style="color:rgb(13, 13, 13);">API</font> 的功能是否按照预期工作。与单元测试不同,集成测试通常在库的外部编写,它们使用库的同样方式来调用公共 <font style="color:rgb(13, 13, 13);">API</font>,就像其他代码一样。这有助于确保你的库作为一个整体正常工作,并且用户能够正确地使用你的 <font style="color:rgb(13, 13, 13);">API</font>。
<font style="color:rgb(13, 13, 13);">Rust</font> 的 <font style="color:rgb(13, 13, 13);">Cargo</font> 工具为集成测试提供了专门的支持。默认情况下,集成测试文件放在项目目录的 **tests** 目录中。每个文件都被当作一个单独的集成测试套件来编译。
**cargo test** 命令时,<font style="color:rgb(13, 13, 13);">Cargo</font> 不仅会运行单元测试,还会编译并运行 **tests** 目录下的每个测试文件。
├── Cargo.lock├── Cargo.toml├── src│ └── lib.rs└── tests└── sum.rs
use integration_test::sum;#[test]fn sum_test() {assert_eq!(sum(6, 8), 14);}
代码中测试的 sum方法必须为公有方法,sum.rs才有权限调用测试。
