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

[Feature]: #[shuttle_runtime::test] macro #1925

Open
1 task done
jonaro00 opened this issue Nov 28, 2024 · 0 comments
Open
1 task done

[Feature]: #[shuttle_runtime::test] macro #1925

jonaro00 opened this issue Nov 28, 2024 · 0 comments
Labels
S-Accepted This will be worked on S-Investigation This issue needs further investigation or design to figure out a solution T-Feature Request A request for a new feature

Comments

@jonaro00
Copy link
Member

Describe the feature

Would allow running integration tests with Shuttle resources, by using similar logic to cargo-shuttle's local provisioner

Suggestion or Example of how the feature would be used

Most of the rust code copied from axum testing example

# Cargo.toml
[dependencies]
shuttle-runtime = "0.x.0"

[dev-dependencies]
# feature gate the test macro since it will likely pull in more deps
shuttle-runtime = { version = "0.x.0", features = ["test"] }
// lib.rs

/// Having a function that produces our app makes it easy to call it from tests
/// without having to create an HTTP server.
fn app(state: AppState) -> Router {
    Router::new()
        .route("/", get(|| async { "Hello, World!" }))
        .with_state(state)
}
use axum::{
    body::Body,
    http::{Request, StatusCode},
};
use http_body_util::BodyExt; // for `collect`
use tower::{Service, ServiceExt}; // for `oneshot`

#[shuttle_runtime::test]
fn test(
    // can the underlying provisioner use `testcontainers` instead of the normal local provisioner's `bollard` calls?
    #[shuttle_shared_db::Postgres] pool: PgPool,
) {
    let app = app();

    // `Router` implements `tower::Service<Request<Body>>` so we can
    // call it like any tower service, no need to run an HTTP server.
    let response = app
        .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
        .await
        .unwrap();

    assert_eq!(response.status(), StatusCode::OK);

    let body = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(&body[..], b"Hello, World!");
}

One approach could be to bake the testing logic into the runtime somehow.
Or it could be done similarly to shuttle run as a shuttle test command that runs only the shuttle-annotated tests.

Duplicate declaration

  • I have searched the issues and this feature has not been requested before.
@jonaro00 jonaro00 added T-Feature Request A request for a new feature S-Triage Awaiting decision for what to do S-Investigation This issue needs further investigation or design to figure out a solution S-Accepted This will be worked on and removed S-Triage Awaiting decision for what to do labels Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-Accepted This will be worked on S-Investigation This issue needs further investigation or design to figure out a solution T-Feature Request A request for a new feature
Projects
None yet
Development

No branches or pull requests

1 participant