xxx · ix · mmxxvi

Projects

pact-python

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

View on GitHub ↗
stars
684
forks
147
watchers
684
open issues
19
latest release
pact-python/3.4.1 · 17 Sep 2026
pull requests
8 open PRs

Recent commits

  • 8c85586chore(ci): standardize renovate configJP-Ellis · 1w ago
  • 58fae68feat: update bindings to Pact FFI 0.5.8JP-Ellis · 1w ago
  • 6401b12fix: declare the plugin log callback as a function pointerJP-Ellis · 1w ago
  • b570668chore(renovate): rebase PRs only when conflictedJP-Ellis · 1w ago
  • 782eb4efix: avoid index error on trailing datetime fmtJP-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.