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-8.0.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

Metadata

Release files for testcontainers-mockserver 9.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for testcontainers-mockserver 9.0.0
File Size Uploaded
testcontainers_mockserver-9.0.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for testcontainers-mockserver 9.0.0
File Interpreter ABI Platform
testcontainers_mockserver-9.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / testcontainers_mockserver-9.0.0.tar.gz

Download URL testcontainers_mockserver-9.0.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0ebbba0dfaa1dbbd6b5b4d0282893e39e2a55c8b6f38d78a6a61d389bfb468ca
BLAKE2b-256 checksum
How to use checksums
8a7c29e24909a1b7cc96b7c3fcf673ae485b2167bc11679bcb6313b20063b1ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / testcontainers_mockserver-9.0.0-py3-none-any.whl

Download URL testcontainers_mockserver-9.0.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f2c795a9a7fb178b810732faec95cf4e08fc1e27f2895b7794d62ade012d66fe
BLAKE2b-256 checksum
How to use checksums
247f128770d1763c1a39212e128c4b3d3c786816e1e9df2913863bcdff584013
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release history Release notifications | RSS feed

This release

9.0.0 This release

2 release files

8.0.0

2 release files

7.6.0

2 release files

7.5.0

2 release files

7.4.0

2 release files

7.3.0

2 release files

7.2.0

2 release files

7.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page