{"id":7866,"library":"wiremock","title":"WireMock Python Client","description":"The `wiremock` library provides a Python client for the WireMock Admin API, enabling programmatic control and configuration of WireMock standalone servers. It allows developers to define stub mappings, reset server state, and verify requests from Python tests or scripts. The current version is 2.7.0, with an active release cadence, often aligning with updates to the WireMock Java core.","status":"active","version":"2.7.0","language":"python","source_language":"en","source_url":"https://github.com/wiremock/wiremock-python","tags":["testing","mocking","api","http","wiremock","integration-testing"],"install":[{"cmd":"pip install wiremock","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for making HTTP requests to the WireMock Admin API and for verifying stubs.","package":"requests","optional":false}],"imports":[{"wrong":"from wiremock.client import WireMockClient","symbol":"f","correct":"from wiremock import f"},{"wrong":"from wiremock.client import WireMockClient","symbol":"os","correct":"from wiremock import os"}],"quickstart":{"code":"import os\nimport requests\nfrom wiremock.client import WireMockClient, MappingBuilder, ResponseDefinitionBuilder, HttpMethods\n\n# The URL where your WireMock server is running. Make sure it's started!\n# Example: `java -jar wiremock-standalone-2.35.0.jar --port 8080`\nWIREMOCK_BASE_URL = os.environ.get('WIREMOCK_URL', 'http://localhost:8080')\nclient = WireMockClient(base_url=WIREMOCK_BASE_URL)\n\nprint(f\"Connected to WireMock at {WIREMOCK_BASE_URL}\")\n\n# 1. Clear all existing stub mappings for a clean slate\nclient.reset_mappings()\nprint(\"All previous stub mappings cleared.\")\n\n# 2. Define a new stub mapping for a GET request to /hello\nhello_stub = MappingBuilder.get(url_path='/hello') \\\n    .will_return(\n        ResponseDefinitionBuilder.response() \\\n            .with_status(200) \\\n            .with_header('Content-Type', 'text/plain') \\\n            .with_body('Hello from WireMock!')\n    ) \\\n    .build()\n\nclient.create_mapping(hello_stub)\nprint(\"Stub for GET /hello created.\")\n\n# 3. Verify the stub by making a request to the WireMock instance\ntry:\n    response = requests.get(f\"{WIREMOCK_BASE_URL}/hello\")\n    print(f\"Request to /hello received status: {response.status_code}\")\n    print(f\"Response body: {response.text}\")\n    assert response.status_code == 200\n    assert response.text == 'Hello from WireMock!'\n    print(\"Stub verified successfully!\")\nexcept requests.exceptions.ConnectionError as e:\n    print(f\"Error: Could not connect to WireMock server at {WIREMOCK_BASE_URL}. Is it running?\")\n    print(f\"Details: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n\n# Optional: Clean up after verification\n# client.reset_mappings()\n# print(\"All stub mappings cleared again.\")","lang":"python","description":"This quickstart demonstrates how to connect to a WireMock server, clear existing stubs, define a new GET stub for `/hello`, and then verify it by making an HTTP request. Ensure a WireMock Java server is running independently before executing this code."},"warnings":[{"fix":"Migrate your code to use `WireMockClient` and the new fluent API for `MappingBuilder` and `ResponseDefinitionBuilder`. Refer to the official documentation for migration guides.","message":"Version 2.0.0 introduced significant breaking changes, including renaming `AdminClient` to `WireMockClient` and a complete overhaul of the API for defining stub mappings.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Before running your Python code, ensure your WireMock server is active and accessible at the specified `base_url`. Common ways to run it include `java -jar wiremock-standalone-2.35.0.jar --port 8080` or using a Docker container.","message":"The `wiremock` Python client controls a WireMock server; it does not start or manage the server itself. You must have a WireMock Java server running independently (e.g., via JAR, Docker, or standalone).\n\nFailure to start the WireMock server will result in `ConnectionError`.","severity":"gotcha","affected_versions":"All"},{"fix":"Always terminate your `MappingBuilder` and `ResponseDefinitionBuilder` fluent calls with `.build()`. For example: `MappingBuilder.get(...).will_return(ResponseDefinitionBuilder.response()...).build()`.","message":"When defining stub mappings, it's crucial to call `.build()` at the end of the `MappingBuilder` chain to finalize the mapping object before passing it to `client.create_mapping()`. Forgetting `.build()` will lead to incorrect object types or `AttributeError`.","severity":"gotcha","affected_versions":"All"},{"fix":"Use `client.reset_mappings()` to clear only stub mappings, or `client.reset_all()` to clear mappings, requests, and scenarios. This is often done in a `setUp` or `tearDown` method of your test runner.","message":"When running tests, stubs defined in previous tests can interfere with subsequent ones, leading to flaky results. It's good practice to clear WireMock's state between tests.","severity":"gotcha","affected_versions":"All"},{"fix":"Keep both your WireMock Python client and WireMock Java server reasonably up-to-date. If encountering issues with new WireMock features, check the `wiremock-python` changelog and the WireMock Java documentation for compatibility notes.","message":"The Python client's features and compatibility are tied to the version of the WireMock Java Admin API. New features in the Java server might not be immediately available or fully supported by older Python client versions.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'2.7.0':47 'activ':50 'admin':14 'align':54 'allow':26 'api':15,64 'cadenc':52 'client':3,10 'configur':20 'control':18 'core':61 'current':44 'defin':29 'develop':27 'enabl':16 'http':65 'integr':68 'integration-test':67 'java':60 'librari':6 'map':31 'mock':63 'often':53 'programmat':17 'provid':7 'python':2,9,39 'releas':51 'request':37 'reset':32 'script':42 'server':24,33 'standalon':23 'state':34 'stub':30 'test':40,62,69 'updat':56 'verifi':36 'version':45 'wiremock':1,5,13,22,59,66","created_at":"2026-04-16T14:16:46.132969+00:00","updated_at":"2026-04-16T14:16:46.132969+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.7.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://wiremock.org","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/wiremock/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-07-03","last_verified":"2026-07-03","next_check":"2026-08-02","install_tag":null}}