Skip to content

adamtheturtle/openapi-mock

Repository files navigation

openapi-mock

Build Status PyPI

Serve an OpenAPI spec as a mock with respx or responses.

Installation

uv pip install openapi-mock

Or with pip:

pip install openapi-mock

Usage

With respx (httpx)

from http import HTTPStatus

import httpx
import respx

from openapi_mock import add_openapi_to_respx

spec = {
    "openapi": "3.0.0",
    "paths": {"/pets": {"get": {"responses": {"200": {"description": "OK"}}}}},
}
with respx.mock(base_url="https://api.example.com", assert_all_called=False) as m:
    add_openapi_to_respx(mock_obj=m, spec=spec, base_url="https://api.example.com")
    response = httpx.get(url="https://api.example.com/pets")
assert response.status_code == HTTPStatus.OK

With responses (requests)

from http import HTTPStatus

import requests
import responses

from openapi_mock import add_openapi_to_responses

spec = {
    "openapi": "3.0.0",
    "paths": {"/pets": {"get": {"responses": {"200": {"description": "OK"}}}}},
}
with responses.RequestsMock() as rsps:
    add_openapi_to_responses(spec=spec, base_url="https://api.example.com", mock=rsps)
    response = requests.get(url="https://api.example.com/pets", timeout=30)
assert response.status_code == HTTPStatus.OK

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages