Skip to content

Improving the examples - adding tests #3263

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

Open
szabgab opened this issue Mar 12, 2025 · 3 comments
Open

Improving the examples - adding tests #3263

szabgab opened this issue Mar 12, 2025 · 3 comments

Comments

@szabgab
Copy link
Contributor

szabgab commented Mar 12, 2025

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 added a commit to szabgab/axum that referenced this issue Mar 12, 2025
@jplatte
Copy link
Member

jplatte commented Mar 12, 2025

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

@yanns
Copy link
Collaborator

yanns commented Mar 12, 2025

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.

szabgab added a commit to szabgab/axum that referenced this issue Mar 13, 2025
Extract the Router creation.
Return some content to the web client.
Test the routes.

See tokio-rs#3263
szabgab added a commit to szabgab/axum that referenced this issue Mar 13, 2025
- Create function to defined the route to make it testable.
- Return data from the POST request to make it testable.
- Add test.

See tokio-rs#3263
szabgab added a commit to szabgab/axum that referenced this issue Mar 14, 2025
* Move the router definition to its own function.
* Add test
* See tokio-rs#3263
szabgab added a commit to szabgab/axum that referenced this issue Mar 17, 2025
szabgab added a commit to szabgab/axum that referenced this issue Mar 17, 2025
@tidely
Copy link

tidely commented Mar 22, 2025

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.

szabgab added a commit to szabgab/axum that referenced this issue Mar 31, 2025
szabgab added a commit to szabgab/axum that referenced this issue Mar 31, 2025
szabgab added a commit to szabgab/axum that referenced this issue Mar 31, 2025
szabgab added a commit to szabgab/axum that referenced this issue Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants