Replies: 4 comments
-
These are not additional steps. Consider you create an application, you need to create a route and add some context data, such as a database connection, and add some middleware. fn create_app() -> impl Endpoint {
let dbpool = create_db_pool();
Route::new().at("/a", a)
.at("/b", b)
.at("/c", c)
.with(middleware1)
.with(middleware2)
.data(dbpool);
}
// start the server
let app = create_app();
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
// test /a
#[tokio::test]
async fn test_a() {
let app = create_app();
let client = TestClient::new(app);
client.get(...).send().await.assert_status_is_ok();
} |
Beta Was this translation helpful? Give feedback.
-
In |
Beta Was this translation helpful? Give feedback.
-
You can also write a unit test. #[handler]
async fn a() -> String {
....
}
// test /a
#[tokio::test]
async fn test_a() {
let client = TestClient::new(a);
client.get(...).send().await.assert_status_is_ok();
} |
Beta Was this translation helpful? Give feedback.
-
Nice and Clear. Maybe the lack of experience makes me come up with this question. Given this design, I can test Thanks a lot for Poem. |
Beta Was this translation helpful? Give feedback.
-
Hi.
I would like to know the decision behind the design of the testing Poem app.
In other frameworks such as Rocket and Warp (I can't try actix for GraphQL because it always fails when compiling), we do not need to configure the app again. We just run the app and test it.
Given the
Health
handler below:We can test it using Rocket simply by running the app via
Client::tracked
and running the request to desired endpoint:It's a similar thing with Warp.
I don't have a real test code in actix, but looking at my old actix REST API test codebase. It looks similar to Poem.
We need to set up and configure the app first.
This is not a head-to-head comparison, since it is a test for REST API. But I get the idea. Both Poem and Actix need to configure and set up the app before a test.
Thus it needs many additional ceremonies before doing the tests. In Rust, we don't have something such as
conftest.py
for Python Pytest.Doing setup-and-teardown is painful. Sometimes the teardown is skipped because the test function is panicked.
That's why I just make each of my integration tests is independent. I am afraid this means I need to do many setup-and-configure db pool, graphql schema in each test function.
Would you like to share the decision behind this?
Thanks for Poem.
Beta Was this translation helpful? Give feedback.
All reactions