1 个不稳定版本
| 0.3.0 | 2023年4月3日 | 
|---|---|
| 0.2.0 |  | 
| 0.1.0 |  | 
8 在 #teardown 中排名
每月下载 30 次
在 2 个crate中使用 (通过 tearup)
10KB
147 行
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 trait。
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.5MB
~35K SLoC