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:
SchemathesisConfig¶
- class pytest_routes.integrations.schemathesis.SchemathesisConfig[source]¶
Bases:
objectConfiguration 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:
SchemathesisAdapter¶
- class pytest_routes.integrations.SchemathesisAdapter[source]¶
Bases:
objectAdapter for Schemathesis integration.
Provides contract testing capabilities when OpenAPI schema is available. Falls back gracefully when Schemathesis is not installed.
- __init__(app, schema_path='/openapi.json', *, validate_responses=True, checks=None)[source]¶
Initialize the Schemathesis adapter.
- load_schema()[source]¶
Load OpenAPI schema via Schemathesis.
- Return type:
- Returns:
The loaded Schemathesis schema object.
- Raises:
ImportError – If Schemathesis is not installed.
RuntimeError – If schema loading fails.
- get_schema()[source]¶
Get the loaded schema, loading it if necessary.
- Return type:
- Returns:
The Schemathesis schema object.
SchemathesisValidator¶
- class pytest_routes.integrations.SchemathesisValidator[source]¶
Bases:
objectResponse validator using Schemathesis schema validation.
This validator integrates with the existing validation framework to provide schema-based response validation.
- Parameters:
adapter (
SchemathesisAdapter)strict (
bool)
- __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.
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¶
Schemathesis Integration - User guide for Schemathesis integration
Schemathesis Documentation - Official Schemathesis docs