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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mockly_testcontainers-0.12.1.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.1.tar.gz
Algorithm Hash digest
SHA256 742ff2bcfdf58a1fa2ca9752e453fe0cc2e34c193f813ae3d02db56fcaed7eb0
MD5 02ca3314f65d86ef6ad74b97792d1ae4
BLAKE2b-256 d2c8f5480ca8861f5b2cb65004c887bfe29aa8e7d0c79dc250474223bcb91cbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mockly_testcontainers-0.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d72beaf06c9a4155de2510461d74920a18c51c3ff946966c10d0ecab108d0a98
MD5 1c4356d60b5bd5be2e362afb88462609
BLAKE2b-256 2cd05fe7de5f34b70037dd7ececee300f5486090e95d92a089abf3a66f8ce0e7

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