axum icon indicating copy to clipboard operation
axum copied to clipboard

Improving the examples - adding tests

Open szabgab opened this issue 8 months ago • 3 comments

Looking at the examples in the examples folder I see that many don't come with tests. Having tests with the examples will both help make sure that they stay correct as axum changes and it will help the would be developers to learn how to test.

As I see many of the examples have

async fn main() {
   ...
   let app = Router::new()
   ...
}

This, as far as I understand, makes it impossible to use the router in the tests. So I think it would be a good idea to refactor these examples to defined a function

fn app() -> Router {
}

and then write

async fn main() {
   ...
   let app: Router = app();
   ...
}

I'd be happy to do this work for some of the examples, but I'd like to know if you even consider this a good idea.

szabgab avatar Mar 12 '25 15:03 szabgab

We have two testing examples. I think the noise and maintenance effort of adding tests to all examples would be bigger than the benefit.

jplatte avatar Mar 12 '25 15:03 jplatte

For people who would like starting contributing, it can be a nice way to add tests for one example. Step by step, we could reach completion.

yanns avatar Mar 12 '25 16:03 yanns

I think having additional indirection in examples complicates the learning process. I think most people want to learn incrementally and having an additional 50-100 lines of tests on examples equivalent to hello world doesn't really achieve that goal. I'd restrict testing to more advanced examples only. If the goal is to help users write tests for their routers, there are ready examples for that.

Axum tries not to introduce implicit behaviour changes, meaning examples are unlikely to break once written. If there are breaking changes, they can be caught using cargo check --workspace. I think it'd be more beneficial to make the examples seem like examples to cargo as well, so we can run cargo check --examples separately.

tidely avatar Mar 22 '25 07:03 tidely