Integrations

The integrations module provides adapters for external testing tools like Schemathesis.

Schemathesis Integration

The Schemathesis integration provides OpenAPI contract testing capabilities.

pytest_routes.integrations.schemathesis_available()[source]

Check if Schemathesis is installed and available.

Return type:

bool

SchemathesisConfig

class pytest_routes.integrations.schemathesis.SchemathesisConfig[source]

Bases: object

Configuration for Schemathesis integration.

Variables:
  • enabled – Whether Schemathesis mode is enabled.

  • schema_path – Path to fetch OpenAPI schema from the app.

  • validate_responses – Whether to validate response bodies against schema.

  • stateful – Stateful testing mode (‘none’, ‘links’).

  • checks – List of Schemathesis checks to run.

Parameters:
enabled: bool = False
schema_path: str = '/openapi.json'
validate_responses: bool = True
stateful: str = 'none'
checks: list[str]
__init__(enabled=False, schema_path='/openapi.json', validate_responses=True, stateful='none', checks=<factory>)
Parameters:

SchemathesisAdapter

class pytest_routes.integrations.SchemathesisAdapter[source]

Bases: object

Adapter for Schemathesis integration.

Provides contract testing capabilities when OpenAPI schema is available. Falls back gracefully when Schemathesis is not installed.

Parameters:
__init__(app, schema_path='/openapi.json', *, validate_responses=True, checks=None)[source]

Initialize the Schemathesis adapter.

Parameters:
  • app (Any) – The ASGI application.

  • schema_path (str) – Path to the OpenAPI schema endpoint.

  • validate_responses (bool) – Whether to validate response bodies.

  • checks (list[str] | None) – List of Schemathesis checks to run.

property available: bool

Check if Schemathesis is installed.

load_schema()[source]

Load OpenAPI schema via Schemathesis.

Return type:

Any

Returns:

The loaded Schemathesis schema object.

Raises:
get_schema()[source]

Get the loaded schema, loading it if necessary.

Return type:

Any

Returns:

The Schemathesis schema object.

create_contract_test(route)[source]

Create Schemathesis-powered contract test for a route.

Parameters:

route (RouteInfo) – The route to create a test for.

Return type:

Callable | None

Returns:

A test function or None if the route is not found in schema.

validate_response(response, route)[source]

Validate response using Schemathesis.

Parameters:
  • response (Any) – The HTTP response object.

  • route (RouteInfo) – The route information.

Return type:

ValidationResult

Returns:

ValidationResult with validation status.

SchemathesisValidator

class pytest_routes.integrations.SchemathesisValidator[source]

Bases: object

Response validator using Schemathesis schema validation.

This validator integrates with the existing validation framework to provide schema-based response validation.

Parameters:
__init__(adapter, *, strict=False)[source]

Initialize the Schemathesis validator.

Parameters:
  • adapter (SchemathesisAdapter) – The SchemathesisAdapter instance.

  • strict (bool) – If True, fail when Schemathesis is not available.

validate(response, route)[source]

Validate response against OpenAPI schema.

Parameters:
  • response (Any) – The HTTP response object.

  • route (RouteInfo) – The route information.

Return type:

ValidationResult

Returns:

ValidationResult with validation status.

Usage Examples

Basic Usage

from pytest_routes.integrations import (
    SchemathesisAdapter,
    SchemathesisValidator,
    schemathesis_available,
)

# Check if Schemathesis is installed
if schemathesis_available():
    # Create adapter for your ASGI app
    adapter = SchemathesisAdapter(
        app=your_app,
        schema_path="/openapi.json",
        validate_responses=True,
    )

    # Load the OpenAPI schema
    schema = adapter.load_schema()

    # Create a validator
    validator = SchemathesisValidator(adapter, strict=True)

    # Validate a response against the schema
    result = validator.validate(response, route_info)
    if not result.valid:
        print(f"Validation errors: {result.errors}")

Custom Checks Configuration

from pytest_routes.integrations import SchemathesisAdapter

# Configure specific Schemathesis checks
adapter = SchemathesisAdapter(
    app=your_app,
    schema_path="/api/openapi.json",
    validate_responses=True,
    checks=[
        "status_code_conformance",
        "content_type_conformance",
        "response_schema_conformance",
    ],
)

See Also