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.2.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.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mockly_testcontainers-0.12.2.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.2.tar.gz
Algorithm Hash digest
SHA256 40bc262eda5c88765cc0a5da5b5a62b9b296e15f8baf144322df41ea9d753fe8
MD5 7d58ce2e440de591ac6617824124b2b1
BLAKE2b-256 480b026ebd7d8990c5d03ebe49fb0dce0cc015c277cbed9e07ac6f97cff585ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mockly_testcontainers-0.12.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7be4c5ad0ef2748da2fb3e55a38cec6db9971df6178c909bdc40c195fe0a89c7
MD5 0316646b67cded928856e99c2a7c3fbe
BLAKE2b-256 19deb7b9d5113e207e4395d6cc520052190a0a4e05266aeb45eb4e52f7fe7fb3

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