Skip to main content

mockly-testcontainers

Run Mockly in Docker-backed Python tests with testcontainers-python.

The package starts ghcr.io/dever-labs/mockly:latest, waits for the management API to be ready, and provides helpers for mocks, scenarios, faults, and logs.

Requirements

  • Python 3.10+
  • Docker

Install

pip install mockly-testcontainers

Quickstart

import urllib.request

from mockly_testcontainers import Mock, MockRequest, MockResponse, MocklyContainer

with MocklyContainer() as container:
    container.add_mock(
        Mock(
            id="get-user",
            request=MockRequest(method="GET", path="/users/1"),
            response=MockResponse(status=200, body='{"id":1}'),
        )
    )

    with urllib.request.urlopen(f"{container.get_http_base()}/users/1") as response:
        assert response.status == 200
        assert response.read().decode() == '{"id":1}'

When to use the testcontainers module

Use mockly-testcontainers when you want Docker-managed lifecycle, no native binary download, or the same Mockly image in local tests and CI.

Use mockly-driver when you want to run the native Mockly binary directly from Python.

Construction and configuration

MocklyContainer extends testcontainers.core.container.DockerContainer.

API Description
MocklyContainer(image=DEFAULT_IMAGE) Create a container using the default Mockly image, or override it with another image name.
with_inline_config(yaml) Replace /config/mockly.yaml with inline YAML before startup.
start() Start the container and wait for the management API.
stop() Stop the container and clean up the generated config file.

Custom YAML config

with MocklyContainer().with_inline_config("""mockly:
  api:
    port: 9091
protocols:
  http:
    enabled: true
    port: 8090
""") as container:
    ...

Management methods

Method Description
get_http_base() Base URL of the mock HTTP server.
get_api_base() Base URL of the management API.
add_mock(mock) Register a dynamic HTTP mock.
delete_mock(mock_id) Delete a mock by ID.
reset() Remove dynamic mocks, deactivate scenarios, and clear faults.
activate_scenario(scenario_id) Activate a configured scenario.
deactivate_scenario(scenario_id) Deactivate a configured scenario.
set_fault(config) Apply a global HTTP fault.
clear_fault() Remove the active fault.
get_logs() Read request logs as JSON.
clear_logs() Clear stored request logs.

Exported types

The package exports these dataclasses:

  • Mock
  • MockRequest
  • MockResponse
  • FaultConfig

Metadata

Release files for mockly-testcontainers 0.17.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mockly-testcontainers 0.17.0
File Size Uploaded Provenance Actions
mockly_testcontainers-0.17.0.tar.gz 9.4 kB Without provenance Details for mockly_testcontainers-0.17.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mockly-testcontainers 0.17.0
File Interpreter ABI Platform Actions
mockly_testcontainers-0.17.0-py3-none-any.whl Python 3 none any Details for mockly_testcontainers-0.17.0-py3-none-any.whl

Total release size: 16.5 kB

Release files / mockly_testcontainers-0.17.0.tar.gz

Download URL mockly_testcontainers-0.17.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
082b5f1dcc66bc630ea2ac0cbd7ee9d3fe4740e9e1f3ac40ae00d1d70c82ab45
BLAKE2b-256 checksum
How to use checksums
f9312697c4c4b52be093289024302d7dbfef3b9a1d8c685752a9ac46b5c1b46d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / mockly_testcontainers-0.17.0-py3-none-any.whl

Download URL mockly_testcontainers-0.17.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
429763b0c824adedbdab4890c9a93cc004b9fc20328b4a5f850dcb9160200633
BLAKE2b-256 checksum
How to use checksums
1ea000e92bacf57f8057913c9fcbd9a3b19ecb3ec5f21f278959e18450eda121
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for mockly-testcontainers 0.17.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page