Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we add before/after hooks for integration tests? #387

Open
sg552 opened this issue May 18, 2020 · 2 comments
Open

Can we add before/after hooks for integration tests? #387

sg552 opened this issue May 18, 2020 · 2 comments
Labels

Comments

@sg552
Copy link

sg552 commented May 18, 2020

hi , our project is using Actix, which is really fast, however, when writing tests, I found there are many duplications in our tests code. espcially the "init server" code, e.g.

#[actix_rt::test]
async fn test_1() {
    let AppData {
        db_pool,
        redis_actor,
        market,
        orders,
        last_price,
    } = generate_test_data();
    let mut app_service = test::init_service(
        App::new()
            .wrap(Logger::new("%r %s time_consuming: %Dms"))
            .data(db_pool.clone())
            .data(redis_actor.clone())
            .data(market.clone())
            .app_data(orders.clone())
            .app_data(last_price.clone())
            .configure(init_routes),
    )   
    .await;
    let ref_db_connection = &db_pool.get().unwrap();
    // test code begins ......
}


#[actix_rt::test]
async fn test_2() {
    let AppData {
        db_pool,
        redis_actor,
        market,
        orders,
        last_price,
    } = generate_test_data();
    let mut app_service = test::init_service(
        App::new()
            .wrap(Logger::new("%r %s time_consuming: %Dms"))
            .data(db_pool.clone())
            .data(redis_actor.clone())
            .data(market.clone())
            .app_data(orders.clone())
            .app_data(last_price.clone())
            .configure(init_routes),
    )
    .await;
    let ref_db_connection = &db_pool.get().unwrap();

    /* real test code begins ... */
   
}

I know there are many test frameworks such as JUnit, Rspec support before/after hooks, which make the code DIY and clean. In the code above, the JUnit way is to extract the "init service code" to before hook,

I have googled around and found this post: https://medium.com/@ericdreichert/test-setup-and-teardown-in-rust-without-a-framework-ba32d97aa5ab I don't know if this is the official way.

so, how to do before/after hooks in Actix/Rust ?

thanks a lot !

@sg552
Copy link
Author

sg552 commented May 18, 2020

in Rspec ( ruby unit test framework ) , we can define a global variable like this :

def setup  
  @@app_service = test::init_service(
        App::new()
            .wrap(Logger::new("%r %s time_consuming: %Dms"))
            .data(db_pool.clone())
            .data(redis_actor.clone())
            .data(market.clone())
            .app_data(orders.clone())
            .app_data(last_price.clone())
            .configure(init_routes),
    ).await 
end

def my_test1
   response = @@app_service.get("/test1").repsonse
   assert response.body.should == "text from test1 "
end
def my_test2
   response = @@app_service.get("/test2").repsonse
   assert response.body.should == "text from test2 "
end

def tear_down
   // .. 
end

and I define the similar variables in Rust/Actix? thanks a lot !

@robjtede
Copy link
Member

You could define a simple macro with macro_rules and use it to do server set up in each test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants