Skip to main content

testcontainers-mockserver

A Testcontainers module for MockServer in Python.

Starts a mockserver/mockserver Docker container, waits for readiness, and provides convenient accessors for the mapped host, port, and URL.

Installation

pip install testcontainers-mockserver

Usage

from testcontainers_mockserver import MockServerContainer
import requests

with MockServerContainer() as mockserver:
    url = mockserver.get_url()  # e.g. "http://localhost:49152"

    # Create an expectation
    requests.put(f"{url}/mockserver/expectation", json={
        "httpRequest": {"method": "GET", "path": "/hello"},
        "httpResponse": {"statusCode": 200, "body": "world"},
    })

    # Match it
    resp = requests.get(f"{url}/hello")
    assert resp.text == "world"

API

MockServerContainer(image=..., port=1080)

  • image — Docker image to use. Defaults to mockserver/mockserver:mockserver-7.4.0.
  • port — Container port MockServer listens on. Defaults to 1080.

Methods

Method Returns Description
get_client() MockServerClient A cached, ready-wired mockserver client pointed at the mapped host/port
get_url() str HTTP base URL (e.g. http://localhost:49152)
get_secure_url() str HTTPS base URL (same port, https:// scheme)
get_host() str Mapped host IP
get_port() int Mapped host port
with_server_port(port) self Override the listen port
with_log_level(level) self Set MOCKSERVER_LOG_LEVEL
with_property(key, value) self Set any MockServer env var
with_initialization_json(path) self Point to a startup expectations JSON file

Building and Testing

# Install in editable mode with test dependencies
pip install -e .[test]

# Run unit tests (no Docker needed)
pytest tests/test_container_config.py

# Run all tests including integration (requires Docker)
pytest

# Skip Docker-dependent tests
pytest -m "not docker"

Requirements

  • Python >= 3.9
  • Docker (for integration tests and actual usage)
  • testcontainers >= 4.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testcontainers_mockserver-7.5.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

testcontainers_mockserver-7.5.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file testcontainers_mockserver-7.5.0.tar.gz.

File metadata

File hashes

Hashes for testcontainers_mockserver-7.5.0.tar.gz
Algorithm Hash digest
SHA256 62d017ec791706e0fbc0b68a1a9a60bfb014de0d434fc0ed7e6e4ef602e7865f
MD5 49bf326fe64ab4f2724c27ffce192d12
BLAKE2b-256 2ab7c1c25832523adbdae9f34414e020f79e5e8a9ee05ea06aae41bfcc26b0c5

See more details on using hashes here.

File details

Details for the file testcontainers_mockserver-7.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for testcontainers_mockserver-7.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f939d9fe8b4efd4816ce649582ecc48862d408c01a5cf67ee93a60c7f48397f8
MD5 de0dac05fca6355bcc43a8cca4e47d40
BLAKE2b-256 d73c9370fb58cdeff0ff05a2a372aade64e7a5f494b6c907a41d38e5eacf1750

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 Sentry Error logging StatusPage Status page