Skip to content

A Python library for API mocking and testing with Testcontainers module and WireMock

License

Notifications You must be signed in to change notification settings

wiremock/python-wiremock

 
 

Python WireMock Admin API Client

WireMock Logo

Python Wiremock is an HTTP client that allows users to interact with a Wiremock instance from within a Python project.

a Docs

Key Features

WireMock can run in unit tests, as a standalone process or a container. Key features include:

  • Supports most of the major Wiremock features (more on their way soon)
  • Support for testcontainers-python to easily start wiremock server for your tests
  • Support for standalone wiremock JAVA sever

Install as Dependency

To install:

`pip install wiremock`

To install with testing dependencies:

`pip install wiremock[testing]`

To install via Poetry:

`poetry add --extras=testing wiremock`

Quick Start

The preferred way of using WireMock to mock your services is by using the provided WireMockContainer testcontainers-python.

import pytest

from wiremock.testing.testcontainer import wiremock_container

@pytest.fixture(scope="session") # (1)
def wm_server():
    with wiremock_container(secure=False) as wm:

        Config.base_url = wm.get_url("__admin") # (2)

        Mappings.create_mapping(
            Mapping(
                request=MappingRequest(method=HttpMethods.GET, url="/hello"),
                response=MappingResponse(status=200, body="hello"),
                persistent=False,
            )
        ) # (3)
        yield wm


def test_get_hello_world(wm_server): # (4)

    resp1 = requests.get(wm_server.get_url("/hello"), verify=False)

    assert resp1.status_code == 200
    assert resp1.content == b"hello"
  1. Create a pytest fixture to manage the container life-cycle. use fixture scope to control how often the container is created

  2. Set the wiremock sdk config url to the url exposed by the container

  3. Create response and request mappings using the Admin SDK.

  4. Use the wm_server fixture in your tests and make requests against the mock server.

You can read more about Testcontainers support in python-wiremock here.

Examples

There are several example projects included to demonstrate the different ways that wiremock can be used to mock services in your tests and systems. The example test modules demonstrate different strategies for testing against the same "product service" and act as a good demonstration of real world applications to help you get started.

Documentation

wiremock documentation can be found at http://wiremock.readthedocs.org/

Pull Requests

General Rules:

  • All Tests must pass
  • Coverage shouldn't decrease
  • All Pull Requests should be rebased against master before submitting the PR.

Development

Setup the project using poetry.

poetry install

About

A Python library for API mocking and testing with Testcontainers module and WireMock

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 22

Languages