xxi · x · mmxxvi

Projects

pact-python

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

View on GitHub ↗
stars
686
forks
147
watchers
686
open issues
20
latest release
pact-python/4.1.0 · 16 Oct 2026
pull requests
9 open PRs

Recent commits

  • ddbf773refactor!: replace yarl.URL with a str-based URLJP-Ellis · 5d ago
  • 100f4c0refactor(verifier)!: accept only str URLsJP-Ellis · 5d ago
  • a02a73bfeat(types): add a str-based URL typeJP-Ellis · 5d ago
  • efc9811perf: import the message and state servers on demandJP-Ellis · 5d ago
  • b84a225perf: write the version file at build timeJP-Ellis · 5d 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.