{"id":5792,"library":"pact-python","title":"pact-python","description":"pact-python is an active library (current version 3.2.1) providing consumer-driven contract testing capabilities for Python applications. It builds on the Pact Rust FFI library, offering full support for Pact features and ensuring compatibility with other Pact implementations. It sees regular updates, often aligning with major Pact specification changes and core library enhancements.","status":"active","version":"3.2.1","language":"en","source_language":"en","source_url":"https://github.com/pact-foundation/pact-python","tags":["contract testing","microservices","API testing","consumer-driven contracts"],"install":[{"cmd":"pip install pact-python","lang":"bash","label":"Latest stable version"}],"dependencies":[{"reason":"Requires Python 3.10 or newer.","package":"Python","optional":false}],"imports":[{"note":"The `pact.v3` namespace was a temporary module during the transition to v3; the main `pact` module now exposes the v3 API directly.","wrong":"from pact.v3 import Pact","symbol":"Pact","correct":"from pact import Pact"},{"note":"While `Consumer` and `Provider` classes exist, `Pact(consumer='...', provider='...')` is the recommended v3 approach. The old v2 API moved to `pact.v2` (which is deprecated).","wrong":"from pact.v2 import Consumer, Provider","symbol":"Consumer, Provider","correct":"from pact import Consumer, Provider"},{"note":"Used for verifying pacts against a provider service.","symbol":"Verifier","correct":"from pact.verifier import Verifier"}],"quickstart":{"code":"import atexit\nimport unittest\nimport requests\nimport os\nfrom pact import Pact\n\n# Define the client that will interact with the provider\nclass UserClient:\n    def __init__(self, base_url):\n        self.base_url = base_url\n\n    def get_user(self, username):\n        response = requests.get(f\"{self.base_url}/users/{username}\")\n        response.raise_for_status()\n        return response.json()\n\n# Set up Pact for consumer testing\n# Use environment variables for consumer/provider names in CI/CD, or hardcode for local dev\nPACT_MOCK_HOST = os.environ.get('PACT_MOCK_HOST', 'localhost')\nPACT_MOCK_PORT = int(os.environ.get('PACT_MOCK_PORT', '1234'))\n\n# Instantiate Pact with consumer and provider names\npact = Pact(\n    consumer=os.environ.get('PACT_CONSUMER', 'MyConsumer'),\n    provider=os.environ.get('PACT_PROVIDER', 'UserService')\n)\n\n# Start the mock service and register its shutdown\npact.start_service(host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT)\natexit.register(pact.stop_service)\n\nclass GetUserInfoContract(unittest.TestCase):\n    def test_get_ash_user(self):\n        expected_body = {\n            'username': 'Ash',\n            'id': 123,\n            'groups': ['Admin']\n        }\n\n        # Define the interaction\n        (pact\n         .given('User Ash exists and is an administrator')\n         .upon_receiving('a request for Ash')\n         .with_request('GET', '/users/Ash')\n         .will_respond_with(200, headers={'Content-Type': 'application/json'}, body=expected_body))\n\n        # Run the consumer code against the mock service\n        with pact:\n            client = UserClient(pact.uri)\n            result = client.get_user('Ash')\n            self.assertEqual(result, expected_body)\n\nif __name__ == '__main__':\n    unittest.main()","lang":"python","description":"This quickstart demonstrates a basic consumer-side contract test using `pact-python`. It sets up a mock service, defines an expected interaction, and then verifies that a simple Python client correctly interacts with the mock service based on the defined contract. The mock service URL and consumer/provider names are configurable via environment variables for CI/CD compatibility."},"warnings":[{"fix":"Migrate code from the old `pact` module (pre-v3) to the new v3 API. If maintaining old code, it needs to be updated to use the deprecated `pact.v2` namespace (e.g., `from pact.v2 import Pact` instead of `from pact import Pact`). A detailed migration guide is available in the official documentation.","message":"Version 3.x introduces significant breaking changes due to a re-architecture leveraging the Pact Rust FFI library, replacing the older Ruby-based executables and API. The primary `pact` module now exposes the v3 API.","severity":"breaking","affected_versions":"3.0.0 and later"},{"fix":"If your project requires Pact Specification v3, explicitly set it during `Pact` instantiation: `Pact(...).with_specification('V3')`.","message":"The default Pact specification version for new `Pact` instances changed from v3 to v4. While largely backward-compatible, explicitly setting the specification might be necessary if your project relies on specific v3 behaviors.","severity":"breaking","affected_versions":"3.0.0 and later"},{"fix":"Refer to the latest documentation for the updated `given()` method signature and adjust your consumer test code accordingly.","message":"The signature of the `Interaction.given()` method has been simplified, which may require updates to consumer test definitions.","severity":"breaking","affected_versions":"3.0.0 and later"},{"fix":"Any direct imports or usage of `pact.v3.ffi` should be replaced with `pact_ffi`.","message":"The `pact.v3.ffi` module has been removed as of v3.0.0 and is replaced by the standalone `pact_ffi` package.","severity":"deprecated","affected_versions":"3.0.0 and later"},{"fix":"Refactor provider state definitions to use keyword arguments for dynamic data, allowing the provider to set up the necessary state more flexibly.","message":"When defining provider states using `given()`, it's recommended to parameterize the state (e.g., `given(\"user exists\", id=123)`) rather than embedding values directly in the state description (e.g., `given(\"user 123 exists\")`). This makes provider state handlers more reusable and robust.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}