Skip to content

#[shuttle_runtime::test] macro for integration tests #1925

@jonaro00

Description

@jonaro00

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 state = ...;
    let app = app(state);

    // `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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    S-AcceptedThis will be worked onS-InvestigationThis issue needs further investigation or design to figure out a solution

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions