1 个不稳定版本
| 0.3.0 | 2023年4月3日 | 
|---|---|
| 0.2.0 |  | 
| 0.1.0 |  | 
#431 in 测试
24 每月下载量
用于 tearup_macro
23KB
483 行
tearup
一个宏测试辅助工具,帮助您编写集成测试。
基本上
- 在测试之前执行 fn setup()
- 在测试结束后执行 fn teardown()
- 如有需要,可使用 panic catch 和 wait 机制
安装
将以下内容添加到您的 Cargo.toml
[dependencies]
tearup = "0.2"
用法
宏 #[tearup(MyContext)] 执行 setup,然后是注解的方法,最后是 teardown。宏 #[tearup_test(MyContext)] 的行为相同,并在方法上添加了 #[test]。
#[tearup_test(WebContext)]
fn it_should_do_this(address: Address) {
    // assert something
}
#[tearup_test(DbContext)]
async fn it_should_do_that(mut db: DbConnection, address: Address) {
    // assert something
}
为此,您需要实现具有 setup 和 teardown 方法的 Context 特性。
use async_trait::async_trait;
use tearup::tearup_test;
// Define your context
struct YourContext {
    something_you_need_in_teardown: SomethingYouSetup,
}
#[async_trait]
impl Context for YourContext {
    async fn setup(shared_context: &mut SharedContext) -> Self {
        /* --> do your setup here <-- */
        // Register struct that you want to access in your test
        shared_context.register(SomethingYouNeedInTest{});
        // You still can store things in your struct for the treardown step
        Self { something_you_need_in_teardown: SomethingYouSetup{} }
    }
    async fn teardown(mut self, shared_context: &mut SharedContext) {
        /* --> clean your stuff here <-- */
        // You still have access to the shared context
        shared_context.get::<SomethingYouNeedInTest>();
        // Same for self
        self.something_you_need_in_teardown;
    }
}
/// Type you need to access in test (registered in the SharedContext) must implement `Clone`
#[derive(Clone)]
struct SomethingYouNeedInTest;
struct SomethingYouSetup;
您还可以将上下文与 ContextCombinator 结合使用
type Both = ConcurrentContextCombinator<YourContext, AnotherContext>;
#[tearup_test(Both)]
fn it_should_do_this(mut something_you_need_in_test: DbConnection, something_from_the_other_context: Address) {
    // assert something
}
type MoreCombinaison = ConcurrentContextCombinator<YourContext, AnotherContext>;
#[tearup_test(MoreCombinaison)]
fn it_should_do_that(mut something_you_need_in_test: DbConnection, something_from_the_other_context: Address) {
    // assert something
}
示例
依赖
~1.3–3MB
~61K SLoC