Skip to main content

Crate reinhardt_test

Crate reinhardt_test 

Source
Expand description

§Reinhardt Test

Testing utilities for the Reinhardt framework.

§Overview

This crate provides comprehensive testing tools inspired by Django REST Framework, including API clients, request factories, assertions, and TestContainers integration for database testing.

§Features

  • APIClient: HTTP client for making test API requests
  • APIRequestFactory: Factory for creating mock HTTP requests
  • APITestCase: Base test case with common assertions
  • Response Assertions: Status, header, and body assertions
  • Factory: Model factory for generating test data
  • DebugToolbar: Debug panel for inspecting queries and timing
  • WebSocketTestClient: WebSocket connection testing
  • TestContainers: Database containers (PostgreSQL, MySQL, Redis) integration

§Quick Start

§API Client

use reinhardt_test::{APIClient, assert_status};
use hyper::StatusCode;

#[tokio::test]
async fn test_user_list() {
    let client = APIClient::new("http://localhost:8000");

    let response = client.get("/api/users/").await.unwrap();
    assert_status(&response, StatusCode::OK);

    let users: Vec<User> = response.json().await.unwrap();
    assert!(!users.is_empty());
}

§Request Factory

use reinhardt_test::{APIRequestFactory, create_test_request};

#[tokio::test]
async fn test_view_directly() {
    let factory = APIRequestFactory::new();

    // Create a GET request
    let request = factory.get("/api/users/").build();

    // Create a POST request with JSON body
    let request = factory.post("/api/users/")
        .json(&json!({"name": "Alice"}))
        .build();

    // Pass to view handler directly
    let response = my_view(request).await;
}

§Assertions

use reinhardt_test::{assert_status, assert_has_header, assert_header_equals, extract_json};
use hyper::StatusCode;

// Status assertions
assert_status(&response, StatusCode::OK);
assert_status(&response, StatusCode::CREATED);

// Header assertions
assert_has_header(&response, "Content-Type");
assert_header_equals(&response, "Content-Type", "application/json");

// Body extraction
let data: MyStruct = extract_json(&response).await.unwrap();

§TestContainers (Database Testing)

Requires the testcontainers feature:

use reinhardt_test::{with_postgres, PostgresContainer};

#[tokio::test]
async fn test_with_database() {
    with_postgres(|db: PostgresContainer| async move {
        let connection_url = db.connection_url();

        // Run tests against the database
        let pool = create_pool(&connection_url).await;
        // ...
    }).await;
}

§Model Factory

use reinhardt_test::{Factory, FactoryBuilder};

let user = FactoryBuilder::<User>::new()
    .with("name", "Test User")
    .with("email", "test@example.com")
    .build();

§Modules

§Feature Flags

  • testcontainers: Enable TestContainers for database testing
  • static: Enable static file testing utilities
  • wasm: Enable WASM frontend testing utilities
  • wasm-full: Enable WASM testing with full web-sys features
  • server-fn-test: Enable server function testing utilities

Re-exports§

pub use client::APIClient;
pub use client::APIClientBuilder;
pub use client::ClientError;
pub use client::HttpVersion;
pub use debug::DebugEntry;
pub use debug::DebugPanel;
pub use debug::DebugToolbar;
pub use debug::SqlQuery;
pub use debug::TimingInfo;
pub use factory::APIRequestFactory;
pub use factory::RequestBuilder;
pub use fixtures::Factory;
pub use fixtures::FactoryBuilder;
pub use fixtures::FixtureError;
pub use fixtures::FixtureLoader;
pub use fixtures::FixtureResult;
pub use fixtures::api_client_from_url;
pub use fixtures::random_test_key;
pub use fixtures::test_config_value;
pub use fixtures::test_server_guard;
pub use http::assert_has_header;
pub use http::assert_header_contains;
pub use http::assert_header_equals;
pub use http::assert_no_header;
pub use http::assert_status;
pub use http::create_insecure_request;
pub use http::create_request;
pub use http::create_response_with_headers;
pub use http::create_response_with_status;
pub use http::create_secure_request;
pub use http::create_test_request;
pub use http::create_test_response;
pub use http::extract_json;
pub use http::get_header;
pub use http::has_header;
pub use http::header_contains;
pub use http::header_equals;
pub use logging::init_test_logging;
pub use messages::MessagesTestMixin;
pub use messages::assert_message_count;
pub use messages::assert_message_exists;
pub use messages::assert_message_level;
pub use messages::assert_message_tags;
pub use messages::assert_messages;
pub use mock::CallRecord;
pub use mock::MockFunction;
pub use mock::SimpleHandler;
pub use mock::Spy;
pub use resource::AsyncTeardownGuard;
pub use resource::AsyncTestResource;
pub use resource::SuiteGuard;
pub use resource::SuiteResource;
pub use resource::TeardownGuard;
pub use resource::TestResource;
pub use resource::acquire_suite;
pub use response::ResponseExt;
pub use response::TestResponse;
pub use server::BodyEchoHandler;
pub use server::DelayedHandler;
pub use server::EchoPathHandler;
pub use server::LargeResponseHandler;
pub use server::MethodEchoHandler;
pub use server::RouterHandler;
pub use server::StatusCodeHandler;
pub use server::shutdown_test_server;
pub use server::spawn_test_server;
pub use testcase::APITestCase;
pub use views::ApiTestModel;
pub use views::ErrorKind;
pub use views::ErrorTestView;
pub use views::SimpleTestView;
pub use views::TestModel;
pub use views::create_api_test_objects;
pub use views::create_json_request;
pub use views::create_large_test_objects;
pub use views::create_request as create_view_request;
pub use views::create_request_with_headers;
pub use views::create_request_with_path_params;
pub use views::create_test_objects;
pub use viewsets::SimpleViewSet;
pub use viewsets::TestViewSet;
pub use websocket::WebSocketTestClient;
pub use assertions::*;

Modules§

assertions
Additional assertion helpers for testing
client
API Client for testing
debug
factory
Request factory for creating test requests
fixtures
Test fixtures and utilities for Reinhardt framework testing
http
HTTP test utilities for Reinhardt framework
logging
Test logging utilities for Reinhardt framework
messages
Message assertion utilities for testing
mock
prelude
Re-export commonly used testing types
resource
Test resource management with automatic setup and teardown
response
Test response wrapper with assertion helpers
server
HTTP server test utilities
testcase
Base test case with common setup and assertions
views
View test utilities for Reinhardt framework
viewsets
Test ViewSet implementations for testing
websocket
WebSocket test client and utilities for integration testing

Macros§

authenticated_test_case
Helper macro for defining authenticated test cases
impl_test_model
Helper macro for implementing Model trait with empty Fields for test models
test_case
Helper macro for defining test cases with automatic setup/teardown

Structs§

ServerRouter
Unified router with hierarchical routing support

Functions§

poll_until
Poll a condition until it becomes true or timeout is reached.