Skip to main content

Python client for Mockly — start/stop servers and manage HTTP mocks in tests

Project description

mockly-driver

Python client for Mockly — download, start, and control a Mockly mock HTTP server from your Python/pytest test suite.


Table of Contents


Installation

pip install mockly-driver

Then download the Mockly binary once:

mockly-install
# binary is placed in ./bin/mockly (or ./bin/mockly.exe on Windows)

Quick Start

pytest fixture

import pytest
from mockly_driver import MocklyServer, Mock, MockRequest, MockResponse

@pytest.fixture(scope="session")
def mockly():
    # Downloads binary if needed, then starts the server
    server = MocklyServer.ensure()
    yield server
    server.stop()

def test_my_service(mockly):
    mockly.add_mock(Mock(
        id="get-users",
        request=MockRequest(method="GET", path="/users"),
        response=MockResponse(status=200, body='[{"id":1}]',
                              headers={"Content-Type": "application/json"}),
    ))

    # Your service under test talks to mockly.http_base
    import urllib.request
    with urllib.request.urlopen(f"{mockly.http_base}/users") as resp:
        assert resp.status == 200

    mockly.reset()

Scenarios

from mockly_driver import Scenario, ScenarioPatch

server = MocklyServer.ensure(
    scenarios=[
        Scenario(
            id="slow-api",
            name="Slow API",
            patches=[ScenarioPatch(mock_id="get-users", delay="2s")],
        )
    ]
)

server.activate_scenario("slow-api")
# ... run slow-path tests ...
server.deactivate_scenario("slow-api")

Faults

from mockly_driver import FaultConfig

server.set_fault(FaultConfig(enabled=True, status_override=503, error_rate=0.5))
# ... run fault-tolerance tests ...
server.clear_fault()

API Reference

MocklyServer.ensure(scenarios=None, **install_kwargs) -> MocklyServer

Downloads the binary (if not already present) and starts the server. install_kwargs are forwarded to install() — see below.

MocklyServer.create(scenarios=None) -> MocklyServer

Starts the server using an already-installed binary. Raises RuntimeError if no binary is found.

server.stop() -> None

Kills the Mockly process and removes the temp config file.

server.add_mock(mock: Mock) -> None

Registers a new HTTP mock at runtime.

Mock(
    id="my-mock",
    request=MockRequest(method="POST", path="/echo",
                        headers={"X-Api-Key": "secret"}),
    response=MockResponse(status=201, body='{"ok":true}',
                          headers={"Content-Type": "application/json"},
                          delay="10ms"),
)

server.delete_mock(mock_id: str) -> None

Removes a previously registered mock by ID.

server.reset() -> None

Removes all dynamic mocks, deactivates all scenarios, and clears any active fault.

server.activate_scenario(scenario_id: str) -> None

Activates a pre-configured scenario.

server.deactivate_scenario(scenario_id: str) -> None

Deactivates a scenario.

server.set_fault(config: FaultConfig) -> None

Enables a fault injection policy.

FaultConfig(
    enabled=True,
    delay="200ms",        # add latency to all responses
    status_override=503,  # force every response to 503
    error_rate=0.3,       # randomly fail 30% of requests
)

server.clear_fault() -> None

Removes the active fault policy.

install(version=None, base_url=None, bin_dir=None, force=False) -> str

Downloads the Mockly binary for the current platform. Returns the path.

get_binary_path(bin_dir=None) -> str | None

Returns the path to an existing binary, or None if not found.


Environment Variables

Variable Description Default
MOCKLY_BINARY_PATH Absolute path to a pre-staged binary — skips all download logic
MOCKLY_VERSION Binary version to download v0.1.0
MOCKLY_DOWNLOAD_BASE_URL Override the GitHub releases base URL (for mirrors/Artifactory) https://github.com/dever-labs/mockly/releases/download
MOCKLY_NO_INSTALL If set, install() raises RuntimeError instead of downloading
HTTPS_PROXY / HTTP_PROXY Route downloads through an HTTP proxy

Proxy / Artifactory / Air-gap

HTTP/HTTPS proxy

export HTTPS_PROXY=http://proxy.corp.example.com:3128
mockly-install

Internal mirror / Artifactory

Set MOCKLY_DOWNLOAD_BASE_URL to your internal artifact repository:

export MOCKLY_DOWNLOAD_BASE_URL=https://artifactory.corp.example.com/mockly/releases/download
mockly-install

Air-gap / pre-staged binary

Copy the binary for your platform to a known location and set:

export MOCKLY_BINARY_PATH=/opt/mockly/mockly
export MOCKLY_NO_INSTALL=1   # optional: prevents accidental downloads

CLI

mockly-install [--version VERSION] [--base-url URL] [--bin-dir DIR] [--force]

Downloads the platform binary to <bin-dir>/mockly[.exe] (default: ./bin/).


License

MIT © dever-labs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mockly_driver-0.7.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mockly_driver-0.7.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file mockly_driver-0.7.0.tar.gz.

File metadata

  • Download URL: mockly_driver-0.7.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for mockly_driver-0.7.0.tar.gz
Algorithm Hash digest
SHA256 52619b6e2d38718db5d4c4eb00806e54453319a446ec303441f1b61899f8c7c1
MD5 7bd17729069452bb10beb3994738dac0
BLAKE2b-256 aca679a2d686bf6f63bb028fe2db7f96944b3b4d14e6955009a5d6b5a3a3befa

See more details on using hashes here.

File details

Details for the file mockly_driver-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: mockly_driver-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for mockly_driver-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48e7151b22e5754c215bc8b4c675a9d03c6bf8f37ec0491547d408de886a2326
MD5 00ba302a53491f9db589875c244ea1ab
BLAKE2b-256 d268e87029e0b0c2b0ce3455b7a62c2a296bbb8276e7e5b8e124a450223c4bba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page