Major update to the WireMock Python library and new Testcontainers module

Oleg Nenashev
Developer Advocate and Community Builder
July 24, 2023

Read on to discover what's the hiss and step-by-step instructions

We have quite a lot of big updates to share about the Python WireMock library! After being dormant for a few years, it has just got a new release with a number of big changes:

  • Enhanced support for WireMock Standalone instances and feature alignment
  • Preview support for Testcontainers Python integration
  • New documentation on ReadTheDocs, powered by MkDocs

Apart from the above, there has been a major effort behind the scenes to refresh the component and to align it with modern Python developer tooling, including new CI pipelines, test coverage, and release automation. The component is now fully ready for future maintenance 💪

Many thanks to Mike Waites for working on the listed changes! At WireMock Inc. we’ve been happy to sponsor this work to enable thousands of Python developers to try out and adopt WireMock. Also, kudos to Cody Lee who is the original creator of the project, and who has supported us during the updates in recent months.

So, WireMock for Python?

WireMock was started as an open source Java test library, but it is no longer a JVM-only project. The WireMock Ecosystem spans many technology stacks and frameworks; there are 50+ different implementations and adapters just on GitHub. Python is one of the most popular programming languages in the world, and so is naturally no exception. 

WireMock on Python originally started as a Python implementation of an API Client by Cody Lee.  He later added support for calling the JAR file directly on local machines to provision and manage the instances, hence providing similar capabilities to plain Java implementation of WireMock. There are also other projects that integrate other popular testing frameworks like the Robot Framework.

Python WireMock with a new logo, the derived work approved by the Python Software Foundation

A few years ago, Python WireMock was donated by Cody to the WireMock community, and refreshing it has been on our roadmap for a long time. This May, the implementation started. Mike Waites, an experienced Python developer, joined the community as a contributor working on the library and expanding its functionality. In June we had a first release with the refresh for the new dependencies and development tooling, and now we are happy to present the new feature releases with one but very big change.

Please welcome the Testcontrainers integration!

Testcontainers module - Preview

Testcontainers is a popular tool for integration testing with containers, eliminating the need for pre existing environments. It provides lightweight, throwaway container instances of common databases, web browsers, developer services, or anything that can run in a Docker container. The Testcontainers framework has many implementations, including Java, Golang and of course Python. You can find more information on its website.

A few months ago we announced the new WireMock module for Testcontainers Java, and it has been just the first stop. After validating the idea we started experimenting with other technology stacks to make WireMock available to more users. Last week, we announced the module for Golang. And now we are happy to announce the ready to fly module for Testcontainers Python! 

Testcontainers and WireMock

The Testcontainers Python module is a part of the Python WireMock library, so a single library integrates both the CLI client and the Testcontainers module. See this page for all documentation and examples.

Try out the Testcontainers module!

We are looking for more users to try out Python WireMock and the Testcontainers module and to share their feedback. Below you can find a step-by-step guide where we will use the pytest framework. If you do not want to follow it step-by-step, you can find the same examples within the project’s repository.

Prerequisites

  • Python 3.7 or above
  • Pip 20.0.0 or above (use apt install python3-pip, for example)
  • Pytest 7.3.0 or above (use pip install pytest)
  • Testcontainers 3.5.0 or above (use pip install testcontainers)

Step 1. Install Python WireMock

Python WireMock is available on the PyPi public repository: https://pypi.org/project/wiremock/ . To install it, just use the Pip - a package installer for Python that uses PyPi  by default:

pip install wiremock

Step 2. Create a Test fixture in your project

As a first step, we will need to provision a test WireMock server to be used in tests. We will define a test fixture that initializes the WireMock container and adds a mapping for the “/hello” endpoint that returns HTTP 200 with “hello” in the body:

  1. Create a new "test.py" file
  2. In this file, add a pytest fixture to manage the container life-cycle. Use fixture scope to control how often the container is created
  3. Set the WireMock SDK config URL to the URL exposed by the container. It will route all Admin API requests to the mock server.
  4. Create REST API stub mapping for the /hello endpoint using the Admin SDK.
import pytest
import requests

from wiremock.testing.testcontainer import wiremock_container
from wiremock.constants import Config
from wiremock.client import *

@pytest.fixture # (1)
def wiremock_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

Step 3. Add a first unit test!

Use the wiremock_server fixture in your tests and make requests against the mock server. Add the following test to the test.py file:

def test_get_hello_world(wiremock_server): # (4)
    response = requests.get(wiremock_server.get_url("/hello"))

    assert response.status_code == 200
    assert response.content == b"hello"

Step 4. Just run the test!

In your test directory, run the following command: pytest test.py -v. Sample output:

Console output of pytest

Share your feedback!

There are a lot of changes that are coming up, for example deeper integrations with PyTest and probably other Python frameworks. Your feedback will be super useful!

  • For reporting any feature requests and issue reports regarding the python ecosystem, please use GitHub Issues here.
  • We also invite you to join the WireMock community Slack which has a `#wiremock-python` channel for all things API mocking in Python.

See the documentation for more details and examples: https://wiremock.readthedocs.io/en/latest/testcontainers 

References

/

Latest posts

Have More Questions?