### Install Python WireMock Library Source: https://github.com/wiremock/python-wiremock/blob/master/docs/quickstart.md This command installs the latest version of the Python WireMock library using pip. It ensures that the necessary dependencies are available for developing and running WireMock-based tests. ```bash pip install wiremock ``` -------------------------------- ### Install WireMock Python Library and Run Tests Source: https://github.com/wiremock/python-wiremock/blob/master/examples/quickstart/README.md This snippet provides a quick way to get started with the WireMock Python integration. It demonstrates how to install the `wiremock` Python library using pip and then execute tests with pytest, assuming a `test.py` file exists. ```bash pip install wiremock pytest test.py -v ``` -------------------------------- ### Install Python WireMock from Git Repository with Poetry Source: https://github.com/wiremock/python-wiremock/blob/master/docs/install.md These commands clone the Python WireMock repository from GitHub and then install its dependencies using Poetry. This method is typically used for development, contributing to the project, or when working with a specific version not yet released on PyPI. ```Bash git clone [TODO](https://github.com/wiremock/python-wiremock.git) poetry install ``` -------------------------------- ### Create Pytest Fixture for WireMock Container and Stub Mapping Source: https://github.com/wiremock/python-wiremock/blob/master/docs/quickstart.md This Python code defines a pytest fixture that provisions a WireMock test container, configures the WireMock SDK's base URL to point to the container's admin API, and creates a GET /hello stub mapping. The fixture ensures the WireMock server is ready and pre-configured for tests. ```python import pytest import requests from wiremock.testing.testcontainer import wiremock_container from wiremock.constants import Config from wiremock.client import * @pytest.fixture def wiremock_server(): with wiremock_container(secure=False) as wm: Config.base_url = wm.get_url("__admin") Mappings.create_mapping( Mapping( request=MappingRequest(method=HttpMethods.GET, url="/hello"), response=MappingResponse(status=200, body="hello"), persistent=False, ) ) yield wm ``` -------------------------------- ### Install Python WireMock with Pip Source: https://github.com/wiremock/python-wiremock/blob/master/docs/install.md This command installs the Python WireMock library using pip, the standard Python package installer. It fetches the latest stable version from PyPI, making it available for use in your Python projects. ```Bash pip install wiremock ``` -------------------------------- ### Run Pytest Tests Source: https://github.com/wiremock/python-wiremock/blob/master/docs/quickstart.md This command executes the pytest tests defined in the 'test.py' file with verbose output. It initiates the test runner, discovers the test functions, and reports their execution status. ```bash pytest test.py -v ``` -------------------------------- ### Install Python WireMock with Testing Dependencies using Pip Source: https://github.com/wiremock/python-wiremock/blob/master/docs/install.md This command installs the Python WireMock library along with its testing dependencies using pip. The `[testing]` extra ensures all necessary packages for running tests are included, which is useful for development or contributing to the project. ```Bash pip install wiremock[testing] ``` -------------------------------- ### Add Python WireMock with Testing Dependencies using Poetry Source: https://github.com/wiremock/python-wiremock/blob/master/docs/install.md This command adds the Python WireMock library to a Poetry project, including its testing dependencies. The `--extras=testing` flag ensures that all packages required for testing are installed alongside the main library, facilitating development and testing workflows. ```Bash poetry add --extras=testing wiremock ``` -------------------------------- ### Write Pytest for WireMock Stub Verification Source: https://github.com/wiremock/python-wiremock/blob/master/docs/quickstart.md This Python pytest function demonstrates how to use the `wiremock_server` fixture to make an HTTP GET request to the mocked /hello endpoint. It then asserts that the response status code is 200 and the body content matches the 'hello' string defined in the stub mapping. ```python def test_get_hello_world(wiremock_server): response = requests.get(wiremock_server.get_url("/hello")) assert response.status_code == 200 assert response.content == b"hello" ``` -------------------------------- ### Run Python Wiremock End-to-End Tests with Docker Compose Source: https://github.com/wiremock/python-wiremock/blob/master/examples/intro/README.md This command executes the end-to-end tests for the python-wiremock example using Docker Compose. It starts the necessary containers and runs pytest within the 'overview_srv' service, configured to display a short traceback on test failures. ```shell docker compose run overview_srv pytest --tb=short ``` -------------------------------- ### Serve MkDocs Documentation Locally Source: https://github.com/wiremock/python-wiremock/blob/master/docs/CONTRIBUTING.md This command starts a built-in development server for MkDocs, allowing contributors to preview documentation changes in real-time. It typically deploys the live documentation site to http://localhost:8000 for easy local access and verification. ```bash mkdocs serve ``` -------------------------------- ### Add Python WireMock with Poetry Source: https://github.com/wiremock/python-wiremock/blob/master/docs/install.md This command adds the Python WireMock library to a project managed by Poetry, a dependency management and packaging tool for Python. Poetry handles dependency resolution and virtual environments automatically. ```Bash poetry add wiremock ``` -------------------------------- ### Install Python Project Dependencies with Poetry Source: https://github.com/wiremock/python-wiremock/blob/master/docs/CONTRIBUTING.md This command installs all necessary project dependencies using Poetry, setting up the development environment after cloning the repository. It ensures all required packages are available for local development and testing. ```bash poetry install ``` -------------------------------- ### Start WireMock Server using Python Context Manager Source: https://github.com/wiremock/python-wiremock/blob/master/docs/api-client.md Illustrates how to start and stop a WireMock server using Python's 'with' statement (context manager). This approach automatically handles server startup and shutdown, and configures the client base URL to the dynamically assigned port. ```python from wiremock.constants import Config from wiremock.client import * from wiremock.server.server import WireMockServer with WireMockServer() as wm: Config.base_url = 'http://localhost:{}/__admin'.format(wm.port) Mappings.create_mapping(...) # Set up stubs requests.get(...) # Make API calls ``` -------------------------------- ### Direct WireMockContainer Instantiation and Chained Configuration Source: https://github.com/wiremock/python-wiremock/blob/master/docs/testcontainers.md This snippet demonstrates the direct instantiation of `WireMockContainer` and its fluent API for chained configuration. It shows how to add multiple mappings, include response body files, and pass command-line arguments or environment variables to the WireMock server, offering fine-grained control over the container setup. ```python WireMockContainer(verify_ssl_certs=False) .with_mapping( "hello-world.json", { "request": {"method": "GET", "url": "/hello"}, "response": {"status": 200, "body": "hello"}, }, ) .with_mapping( "hello-world-file.json", { "request": {"method": "GET", "url": "/hello2"}, "response": {"status": 200, "bodyFileName": "hello.json"}, }, ) .with_file("hello.json", {"message": "Hello World !"}) .with_cli_arg("--verbose", "") .with_cli_arg("--root-dir", "/home/wiremock") .with_env("JAVA_OPTS", "-Djava.net.preferIPv4Stack=true") ``` -------------------------------- ### Configure WireMock Container with Mappings via Context Manager Source: https://github.com/wiremock/python-wiremock/blob/master/docs/testcontainers.md This example illustrates how to use the `wiremock_container` context manager to automatically configure request and response mappings by passing them directly as a list. It simplifies the setup of stubs by allowing them to be defined inline or loaded from a structure, and then verifies the mapping by making a request. ```python @pytest.mark.container_test def test_configure_via_wiremock_container_context_manager(): mappings = [ ( "hello-world.json", { "request": {"method": "GET", "url": "/hello"}, "response": {"status": 200, "body": "hello"}, }, ) ] with wiremock_container(mappings=mappings, verify_ssl_certs=False) as wm: resp1 = requests.get(wm.get_url("/hello"), verify=False) assert resp1.status_code == 200 assert resp1.content == b"hello" ``` -------------------------------- ### Configure WireMock Client and Create Mapping in Python Source: https://github.com/wiremock/python-wiremock/blob/master/docs/api-client.md Demonstrates how to configure the WireMock Python client's base URL and create a simple GET request mapping with a 200 OK response. It also shows how to retrieve all existing mappings from the WireMock server. ```python from wiremock.constants import Config from wiremock.client import * Config.base_url = 'https://mockserver.example.com/__admin/' # Optionally set a custom cert path: # Config.requests_cert = ... (See requests documentation) # Optionally disable cert verification # Config.requests_verify = False mapping = Mapping( priority=100, request=MappingRequest( method=HttpMethods.GET, url='/hello' ), response=MappingResponse( status=200, body='hi' ), persistent=False, ) mapping = Mappings.create_mapping(mapping=mapping) all_mappings = Mappings.retrieve_all_mappings() ``` -------------------------------- ### Integrate WireMock Server with Python unittest.TestCase Source: https://github.com/wiremock/python-wiremock/blob/master/docs/api-client.md Shows how to integrate WireMock server lifecycle management into a Python `unittest.TestCase`. The server is started in the `setUpClass` method and stopped in `tearDownClass`, ensuring the server is available for all tests in the class and properly cleaned up afterwards. ```python class MyTestClassBase(TestCase): @classmethod def setUpClass(cls): wm = self.wiremock_server = WireMockServer() wm.start() Config.base_url = 'http://localhost:{}/__admin'.format(wm.port) @classmethod def tearDownClass(cls): self.wiremock_server.stop() ``` -------------------------------- ### Build MkDocs Documentation Locally Source: https://github.com/wiremock/python-wiremock/blob/master/docs/CONTRIBUTING.md This command builds the project's documentation using MkDocs, generating static HTML files in the specified 'html' directory. It's executed via Poetry to ensure all documentation dependencies are correctly managed and utilized. ```bash poetry run mkdocs build --site-dir=html ``` -------------------------------- ### Python WireMock Testcontainers API Reference Source: https://github.com/wiremock/python-wiremock/blob/master/docs/testcontainers.md Comprehensive API documentation for the `wiremock_container` context manager and the `WireMockContainer` class, detailing their constructors, parameters, and methods for managing WireMock instances within Python test environments. This includes options for SSL verification, automatic startup, mapping configuration, file injection, and custom CLI/environment variable settings. ```APIDOC wiremock.testing.testcontainer.wiremock_container( secure: bool = False, start: bool = True, mappings: list = None, verify_ssl_certs: bool = True, **kwargs ) -> WireMockContainer - Purpose: A context manager to simplify the lifecycle management of a WireMock container. - Parameters: - secure (bool): If True, the WireMock container will be configured for HTTPS (default: False). - start (bool): If False, the container will not be started automatically upon entering the context (default: True). - mappings (list): A list of tuples, where each tuple is (filename: str, mapping_dict: dict). These mappings are automatically added to the container. - verify_ssl_certs (bool): If False, SSL certificate verification is disabled for internal operations (default: True). - **kwargs: Additional keyword arguments passed directly to the underlying WireMockContainer constructor. - Returns: An instance of WireMockContainer, which is yielded by the context manager. wiremock.testing.testcontainer.WireMockContainer( verify_ssl_certs: bool = True, **kwargs ) - Purpose: Represents a WireMock Docker container instance, providing methods for configuration and interaction. - Parameters: - verify_ssl_certs (bool): If False, SSL certificate verification is disabled for internal operations (default: True). - **kwargs: Additional keyword arguments for container setup (e.g., image, port bindings). - Methods (Chainable): - .with_mapping(filename: str, mapping_dict: dict) -> WireMockContainer - Adds a WireMock mapping definition to the container. The mapping_dict should conform to WireMock's JSON mapping structure. - Parameters: - filename (str): The name of the mapping file (e.g., "my-stub.json"). - mapping_dict (dict): The dictionary representing the WireMock mapping. - .with_file(filename: str, content: Union[str, bytes, dict]) -> WireMockContainer - Adds a file to the WireMock container's file system, typically used for response body files. - Parameters: - filename (str): The path and name of the file within the container (e.g., "__files/response.json"). - content (Union[str, bytes, dict]): The content of the file. If a dict, it will be serialized to JSON. - .with_cli_arg(arg_name: str, arg_value: str = "") -> WireMockContainer - Passes a command-line argument to the WireMock server process. - Parameters: - arg_name (str): The name of the CLI argument (e.g., "--verbose", "--root-dir"). - arg_value (str): The value for the CLI argument (optional). - .with_env(env_var_name: str, env_var_value: str) -> WireMockContainer - Sets an environment variable for the WireMock container. - Parameters: - env_var_name (str): The name of the environment variable (e.g., "JAVA_OPTS"). - env_var_value (str): The value of the environment variable. - Other Methods: - .get_url(path: str = "") -> str - Returns the base URL of the running WireMock container, optionally with a specific path appended. - Parameters: - path (str): An optional path to append to the base URL (e.g., "__admin"). - Returns: The full URL as a string. ``` -------------------------------- ### Customize Java Executable Path for WireMock Server Source: https://github.com/wiremock/python-wiremock/blob/master/docs/api-client.md Demonstrates how to specify a custom path to the Java executable when initializing the `WireMockServer` instance. This is useful if Java is not in the system's PATH or if a specific Java version needs to be used. ```python WireMockServer(java_path='/path/to/my/java') ``` -------------------------------- ### Pytest Fixture for WireMock Container with Admin SDK Mappings Source: https://github.com/wiremock/python-wiremock/blob/master/docs/testcontainers.md This snippet demonstrates how to integrate the WireMock container into a pytest test suite using a session-scoped fixture. It shows how to configure the WireMock SDK's base URL and programmatically create request/response mappings using the Admin SDK, followed by a test making a request against the mock server. ```python import pytest from wiremock.testing.testcontainer import wiremock_container from wiremock.client import Config, Mappings, Mapping, MappingRequest, MappingResponse, HttpMethods import requests @pytest.fixture(scope="session") def wm_server(): with wiremock_container(secure=False) as wm: Config.base_url = wm.get_url("__admin") Mappings.create_mapping( Mapping( request=MappingRequest(method=HttpMethods.GET, url="/hello"), response=MappingResponse(status=200, body="hello"), persistent=False, ) ) yield wm def test_get_hello_world(wm_server): resp1 = requests.get(wm_server.get_url("/hello"), verify=False) assert resp1.status_code == 200 assert resp1.content == b"hello" ``` -------------------------------- ### Specify Custom WireMock Server JAR File Path Source: https://github.com/wiremock/python-wiremock/blob/master/docs/api-client.md Explains how to provide a custom path to the WireMock standalone JAR file when instantiating `WireMockServer`. This allows users to utilize a specific WireMock version or a JAR file located outside the default download path. ```python WireMockServer(jar_path='/my/secret/location/wiremock-standalone-2.35.1.jar') ``` -------------------------------- ### Docker Compose Configuration for WireMockContainer in DIND Source: https://github.com/wiremock/python-wiremock/blob/master/docs/testcontainers.md This `docker-compose.yml` snippet demonstrates how to set up a service that uses WireMockContainer in a Docker-in-Docker (DIND) environment. It configures the `WIREMOCK_DIND` environment variable to `true`, maps `host.docker.internal` to `host-gateway` for host resolution, and mounts the Docker socket to allow the container to spawn other containers. ```yaml version: "3" services: overview_srv: build: context: ../ dockerfile: examples/intro/Dockerfile ports: - "5001:5001" environment: - WIREMOCK_DIND=true # (1) Set the env var extra_hosts: - "host.docker.internal:host-gateway" # (2) volumes: - /var/run/docker.sock:/var/run/docker.sock # (3) - ..:/app/ - .:/app/example/ command: uvicorn product_mock.overview_service:app --host=0.0.0.0 --port=5001 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.