Test provider
Provider contract tests using Pact.
This module demonstrates how to test a FastAPI provider (see
provider.py
) against a mock
consumer using Pact. The mock consumer replays the requests defined by the
consumer contract, and Pact validates that the provider responds as expected.
These tests show how provider verification ensures that the provider remains compatible with the consumer contract as the provider evolves. Provider state management is handled by mocking the database and using provider state endpoints. For more, see the Pact Provider Test section of the Pact documentation.
Attributes¶
ACTION_TYPE: TypeAlias = Literal['setup', 'teardown']
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions¶
app_server() -> str
¶
Run the FastAPI server for provider verification.
RETURNS | DESCRIPTION |
---|---|
str
|
The base URL of the running FastAPI server. |
Source code in examples/http/requests_and_fastapi/test_provider.py
default_mock_db() -> dict[int, User]
¶
Standard in-memory database for provider state mocking.
This function pre-populates a mock database with some default users. It is used by the provider state handlers to ensure that the database is in the correct state for each interaction.
RETURNS | DESCRIPTION |
---|---|
dict[int, User]
|
A dictionary of user IDs to User objects for use in tests. |
Source code in examples/http/requests_and_fastapi/test_provider.py
mock_user_does_not_exist(action: Literal['setup', 'teardown'], parameters: dict[str, Any]) -> None
¶
Mock the provider state where a user does not exist.
This handler sets up the provider so that a user with the given ID does not exist in the database. Used by Pact to ensure the provider is in the correct state for each interaction.
PARAMETER | DESCRIPTION |
---|---|
action
|
The action to perform, either "setup" or "teardown".
TYPE:
|
parameters
|
User information, must contain an |
Source code in examples/http/requests_and_fastapi/test_provider.py
mock_user_exists(action: Literal['setup', 'teardown'], parameters: dict[str, Any]) -> None
¶
Mock the provider state where a user exists.
This handler sets up the provider so that a user with the given ID exists in the database. Used by Pact to ensure the provider is in the correct state for each interaction.
PARAMETER | DESCRIPTION |
---|---|
action
|
The action to perform, either "setup" or "teardown".
TYPE:
|
parameters
|
User information, including an |
Source code in examples/http/requests_and_fastapi/test_provider.py
start_fastapi_server(host: str, port: int) -> None
¶
test_provider(app_server: str, pacts_path: Path) -> None
¶
Test the provider against the mock consumer contract.
This test runs the Pact verifier against the FastAPI provider, using the contract generated by the consumer tests.
Provider state handlers are essential in Pact contract testing. They allow the provider to be set up in a specific state before each interaction is verified. For example, if a consumer expects a user to exist for a certain request, the provider state handler ensures the database is populated accordingly. This enables repeatable, isolated, and meaningful contract verification, as each interaction can be tested in the correct context without relying on global or persistent state.
In this example, the state handlers mock_user_exists
and
mock_user_does_not_exist
are mapped to the states described in the
contract. They are responsible for setting up (and tearing down) the
in-memory database so that the provider can respond correctly to each
request defined by the consumer contract.
For additional information on state handlers, see
Verifier.state_handler
.