Skip to main content

Testcontainers module for Mockly — run Mockly as a Docker container in tests

Project description

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

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_testcontainers-0.12.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

mockly_testcontainers-0.12.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file mockly_testcontainers-0.12.0.tar.gz.

File metadata

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

File hashes

Hashes for mockly_testcontainers-0.12.0.tar.gz
Algorithm Hash digest
SHA256 d6b50b8502f1d868e739f56b51c19f5f611dc37b829c89d17ad5c0a52f8d38dc
MD5 12f2535a97a43c2272d1c5035fcb77d3
BLAKE2b-256 ca0fa6a4d70c6d26d80511f55defed11c5426b408faaf8292f96df97c13cd11b

See more details on using hashes here.

File details

Details for the file mockly_testcontainers-0.12.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mockly_testcontainers-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dee38a75f5b709772f2777e4050dc921215bab40f893f000b88c27bbcfdd4987
MD5 ad9c3cbdfd755298a40c0e8aac11ca80
BLAKE2b-256 5d767cbf75ffd2d59364404b8dbce31b614f47d1ae1c93f2fffd6656d01d5531

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