ii · xi · mmxxvi

Projects

pact-python

Contract testing for Python services, built on a Rust FFI core

View on GitHub ↗
stars
687
forks
147
watchers
687
open issues
19
latest release
pact-python-ffi/0.5.12.1 · 23 Oct 2026
pull requests
9 open PRs

Recent commits

  • d52c8c7fix(ci): keep PR merge runs out of the main release groupJP-Ellis · 1w ago
  • 493c8b1fix(ci): tag the merged release commitJP-Ellis · 1w ago
  • 3da8f7echore(ci): stop release runs on main being droppedJP-Ellis · 1w ago
  • 25e425etest(ffi): measure coverage in subprocess testsJP-Ellis · 1w ago
  • 8c7e753test: import testcontainers only when starting a brokerJP-Ellis · 1w ago

Pact is a contract testing framework. The consumer records the requests it makes and the responses it expects in a pact file. The provider then replays that file against itself and checks each response. Neither test needs the other service running.

pact-python is the Python library in the Pact Foundation ecosystem. I rebuilt it from the ground up over a Rust FFI core (pact-reference), replacing the previous approach of shelling out to a Ruby binary. The result is a library that installs as a plain Python wheel with no system dependencies, supports the full Pact specification (V1–V4), and runs on all major platforms.

Usage

from pathlib import Path

from pact import Pact, match


def test_get_user() -> None:
    pact = Pact("user-consumer", "user-provider")
    (
        pact
        .upon_receiving("a request for user 1")
        .given("user 1 exists")
        .with_request("GET", "/users/1")
        .will_respond_with(200)
        .with_body(
            {"id": match.int(1), "name": match.str("Alice")},
            content_type="application/json",
        )
    )

    with pact.serve() as srv:
        client = UserClient(str(srv.url))  # your actual client code
        assert client.get_user(1).name == "Alice"

    pact.write_file(Path(__file__).parent / "pacts")

The library is published to PyPI.