ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

setup teardown rust如何实现

setup teardown rust如何实现 Rust 里没有 JUnit 那种“全局 before/after”钩子但setup/teardown 通常有三层做法简单场景用Drop测试上下文用test-context复杂套件用rstestfixture 或suitest。1. 最轻量用Drop做 teardownRust 的Drop天然适合做清理连接池、临时文件、测试目录、临时端口绑定都可以靠它回收。struct TempDir { path: std::path::PathBuf, } impl TempDir { fn new() - Self { let path std::env::temp_dir().join(my_test); std::fs::create_dir_all(path).unwrap(); Self { path } } } impl Drop for TempDir { fn drop(mut self) { let _ std::fs::remove_dir_all(self.path); } } #[test] fn test_with_temp_dir() { let dir TempDir::new(); // 使用 dir.path 做测试 }2. 最接近 JUnittest-context如果你想要明确的setup()/teardown()test-context很合适还能配合#[tokio::test]。use test_context::{test_context, AsyncTestContext}; struct AppContext { db_url: String, } #[async_trait::async_trait] impl AsyncTestContext for AppContext { async fn setup() - Self { Self { db_url: postgres://localhost:5432/test.into(), } } async fn teardown(self) { // 清理测试数据库、关闭连接池等 } } #[test_context(AppContext)] #[tokio::test] async fn test_handler(ctx: mut AppContext) { assert!(!ctx.db_url.is_empty()); }3. 参数化 fixturerstestrstest没有显式的beforeEach/afterEach但可以用 fixture 做 setup用Drop或#[once]控制生命周期。use rstest::*; #[fixture] fn db_url() - String { postgres://localhost/test.into() } #[fixture] fn temp_dir() - TempDir { TempDir::new() } #[rstest] fn test_query(db_url: String, temp_dir: TempDir) { assert!(!db_url.is_empty()); }如果 fixture 需要全局共享比如测试数据库连接池可以用#[fixture] #[once] fn pool() - Pool { Pool::new() }4. 需要完整套件钩子suitest/spectacular如果你确实需要before_all / before_each / after_each / after_all可以用suitest。use suitest::{suite, before_all, after_all, before_each, after_each}; #[suite(my_suite)] mod tests { use suitest::*; #[before_all] async fn setup() - usize { 42 } #[after_all] async fn teardown(value: usize) { assert_eq!(value, 42); } #[before_each] fn before() - String { ready.into() } #[after_each] fn after(state: String) { assert_eq!(state, ready); } #[test] fn it_works(state: String, value: usize) { assert_eq!(state, ready); assert_eq!(value, 42); } }选型建议场景推荐临时文件、临时目录、简单资源清理Drop每个测试需要统一上下文test-context参数化测试 fixture 注入rstest需要 before/after 套件钩子suitest/spectacular框架集成测试rstestDropmockall如果是在搭后端框架的测试基础设施建议优先用rstest管理 fixtureDrop管 teardowntest-context补显式 setup/teardown。这样既能保持工程化边界清晰也不会引入太重的测试 harness。需要的话我可以帮你把这套封装成框架里的TestApp和TestFixture基类。
返回列表