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:
MockMockRequestMockResponseFaultConfig
Metadata
Release files for mockly-testcontainers 0.16.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mockly_testcontainers-0.16.0.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mockly_testcontainers-0.16.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.6 kB
Release files / mockly_testcontainers-0.16.0.tar.gz
| Download URL | mockly_testcontainers-0.16.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35e46add6e7d666009439f97069e16ab30554c9713e5103b5c047367ec967f5d
|
|
BLAKE2b-256 checksum How to use checksums |
9bf80495193fa81c3b6ff2bf6f689ba15a144f33424b62acd7f509f00d0bca43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / mockly_testcontainers-0.16.0-py3-none-any.whl
| Download URL | mockly_testcontainers-0.16.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b97b09c339c81a3f5e8ce89830a300b0d86dc04059b372b8f7bf3773952db4e
|
|
BLAKE2b-256 checksum How to use checksums |
d4f40d57ca6c954a42366f2149f1865fb62b53c6a393a4afed82642e3e452f02
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|