MockTown Python SDK
A programmable mock internet for integration testing.
MockTown lets you spin up stateful, scenario-driven mock services (Stripe, AWS S3, Twilio, etc.) on dedicated ports for reproducible, zero-flakiness testing.
Installation
pip install mocktown
Quick Start
Using the Context Manager (Recommended)
from mocktown import MockTown
mt = MockTown(api_key="mt_your_api_key", base_url="http://localhost:8080")
with mt.session("checkout-test") as session:
# Spin up mock Stripe on port 9101
stripe_mock = session.start_mock("stripe", port=9101)
# Point your application / test client to MockTown
import stripe
stripe.api_key = "sk_test_fake"
stripe.api_base = stripe_mock.url # "http://localhost:9101"
# Run your application logic
customer = stripe.Customer.create(email="test@example.com")
assert customer.id.startswith("cus_")
# Inspect internal mock state
assert len(stripe_mock.state["customers"]) == 1
assert stripe_mock.state["customers"][0]["email"] == "test@example.com"
Features
- Context Manager: Sessions and mocks automatically clean up when your test block finishes.
- State Inspection: Inspect internal service state directly via
.stateproperty (e.g.mock.state["customers"],mock.state["objects"]). - Scenario Simulation: Test retry logic, chaos injection, latency delays, and fixed failure responses:
session.start_mock("stripe", scenario="payment_failure_retry", port=9101)
- Record & Replay: Capture live API interactions and replay them in deterministic CI environments.
- Verification Assertions: Run automated assertion suites against mock logs.
Pytest Fixture Example
import pytest
from mocktown import MockTown
@pytest.fixture(scope="session")
def mt():
return MockTown()
@pytest.fixture
def mock_stripe(mt):
with mt.session("test-stripe-session") as session:
mock = session.start_mock("stripe", port=9101)
yield mock
Configuration
The SDK can be configured programmatically or via environment variables:
| Variable | Description | Default |
|---|---|---|
MOCKTOWN_API_KEY |
Bearer API key | — |
MOCKTOWN_BASE_URL |
Core server API endpoint | http://localhost:8080 |
MOCKTOWN_TIMEOUT |
Request timeout in seconds | 30 |
License
MIT License. Copyright (c) 2026 Vikas Budde.
Metadata
Release files for mocktown 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mocktown-1.0.0.tar.gz | 9.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mocktown-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.1 kB
Release files / mocktown-1.0.0.tar.gz
| Download URL | mocktown-1.0.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
af73ef5fa8abc5bf1697bc5d69e476d593d0cd434f61edff35a14b0ec446f6e1
|
|
BLAKE2b-256 checksum How to use checksums |
13420d456fc45bcaaf6b593b43ea5b28133ea431b8e2e40ad1d37e1dea6b8d89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.12
|
Release files / mocktown-1.0.0-py3-none-any.whl
| Download URL | mocktown-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9db46a032adac329d50c4cd01c4ef0cfdc11c34a75ad478e333cbc0ba4f0c4b4
|
|
BLAKE2b-256 checksum How to use checksums |
38a5ff9a8dd48a83ea7bc245cbd064386221f04d7842c3f9376b20b1daac7ae0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.12
|