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.13.1

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.13.1
File Size Uploaded
mockly_testcontainers-0.13.1.tar.gz 8.3 kB Details

Built distribution (wheel)

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

Total release size: 15.2 kB

Release files / mockly_testcontainers-0.13.1.tar.gz

Download URL mockly_testcontainers-0.13.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4fefad679007e191f81d6ef520c7a6be4b04df4bb3f2f67f1290e6bbd8f09487
BLAKE2b-256 checksum
How to use checksums
ae09719167fb1127a650b55e79832c29708b3e61fe55ba7bf5a3472aae4234f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

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

Download URL mockly_testcontainers-0.13.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fdc54bf2af7a5e1d7011dd6af068a84c2e157b36ca28f04252d77526e930576
BLAKE2b-256 checksum
How to use checksums
f34266af76cbb36f758998807c9028004a2c35e32e867b8ee8449565def92c1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

0.16.0

2 release files

This release

0.13.1 This release

2 release files

0.12.4

2 release files

0.12.3

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.0

2 release files

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