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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mockly_testcontainers-0.12.3.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.3.tar.gz
Algorithm Hash digest
SHA256 f5d5d2078e848af8ce30e962783571405783c9277437ab006ef08618974f1632
MD5 a75ee7e24dc618e35520e5ebf4ccf740
BLAKE2b-256 98f32dfa3ff2a6e4a047d90e80ece319ea729d6d2d293f4aa5405827858e351c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mockly_testcontainers-0.12.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1598ee0f4f03e19fe8eebe67cfaf5ab74a901ec5d4392832de015fc882b22cad
MD5 6be4f10db245b3f14970ec057d86a173
BLAKE2b-256 ea20416d34732f896029940144b2de07cd97b2d9b0f874f04de70eedfb18e0fa

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