Test Provider¶
Provider test using Protobuf plugin with Pact Python v3.
This module demonstrates how to write a provider test using the Pact protobuf plugin with Pact Python's v3 API. The provider test verifies that the provider service correctly handles the contract defined by the consumer test.
The provider test runs the actual provider service and uses Pact to replay the consumer's interactions against the provider, verifying that the provider responds correctly with protobuf-serialized messages.
This example shows how to:
- Set up a FastAPI provider that handles protobuf responses
- Use the Pact Verifier with the protobuf plugin
- Handle provider states for setting up test data
- Verify protobuf serialization in the provider responses
Attributes¶
MOCK_ADDRESS_BOOK: AddressBook | None = None
module-attribute
¶
PROVIDER_URL = URL('http://localhost:8001')
module-attribute
¶
app = FastAPI(title='Protobuf Address Book API')
module-attribute
¶
FastAPI application
This application serves as the provider for the address book service, handling requests to retrieve person data by ID. It uses Protocol Buffers for serialization of the response data.
This code would typically be in a separate module within your application, but for the sake of this example, it is included directly within the test module.
Classes¶
Server
¶
Bases: Server
Custom server class to run the FastAPI server in a separate thread.
This allows the provider test to run the FastAPI server in the background while Pact verifies the interactions against it.
Functions¶
install_signal_handlers() -> None
¶
Prevent the server from installing signal handlers.
This is required to run the FastAPI server in a separate process.
run_in_thread() -> Generator[str, None, None]
¶
Run the FastAPI server in a separate thread.
YIELDS | DESCRIPTION |
---|---|
str
|
The URL of the running server. |
Source code in examples/v3/plugins/protobuf/test_provider.py
Functions¶
get_person(person_id: int) -> Response
async
¶
Get a person by ID, returning protobuf-serialized data.
PARAMETER | DESCRIPTION |
---|---|
person_id
|
The ID of the person to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
Response containing protobuf-serialized Person data. |
RAISES | DESCRIPTION |
---|---|
HTTPException
|
If person is not found. |
Source code in examples/v3/plugins/protobuf/test_provider.py
server() -> Generator[str, None, None]
¶
Fixture to start the FastAPI server for testing.
YIELDS | DESCRIPTION |
---|---|
str
|
The URL of the running server. |
Source code in examples/v3/plugins/protobuf/test_provider.py
state_person_doesnt_exist(action: Literal['setup', 'teardown'], parameters: dict[str, Any] | None = None) -> None
¶
Handle provider state for when a person with the given ID doesn't exist.
PARAMETER | DESCRIPTION |
---|---|
action
|
Either "setup" or "teardown".
TYPE:
|
parameters
|
Dictionary containing user_id key. |
Source code in examples/v3/plugins/protobuf/test_provider.py
state_person_exists(action: Literal['setup', 'teardown'], parameters: dict[str, Any] | None = None) -> None
¶
Handle provider state for when a person with the given ID exists.
PARAMETER | DESCRIPTION |
---|---|
action
|
Either "setup" or "teardown".
TYPE:
|
parameters
|
Dictionary containing user_id key. |
Source code in examples/v3/plugins/protobuf/test_provider.py
test_provider(server: str) -> None
¶
Test the protobuf provider against the consumer contract.
This test uses the Pact Verifier to replay the consumer's interactions against the running provider service. It verifies that the provider correctly handles protobuf serialization and responds appropriately to both successful and error scenarios.
The test:
- Configures the Verifier with the protobuf plugin
- Points the verifier to the pact file generated by the consumer
- Sets up state handlers to prepare test data
- Verifies all interactions match the contract