Test 00 Consumer¶
Test the consumer with Pact.
This module tests the consumer defined in src/consumer.py
against a mock
provider. The mock provider is set up by Pact, and is used to ensure that the
consumer is making the expected requests to the provider, and that the provider
is responding with the expected responses. Once these interactions are
validated, the contracts can be published to a Pact Broker. The contracts can
then be used to validate the provider's interactions.
A good resource for understanding the consumer tests is the Pact Consumer Test section of the Pact documentation.
Attributes¶
MOCK_URL = URL('http://localhost:8080')
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions¶
pact(broker: URL, pact_dir: Path) -> Generator[Pact, Any, None]
¶
Set up Pact.
In order to test the consumer in isolation, Pact sets up a mock version of the provider. This mock provider will expect to receive defined requests and will respond with defined responses.
The fixture here simply defines the Consumer and Provider, and sets up the mock provider. With each test, we define the expected request and response from the provider as follows:
pact.given("UserA exists and is not an admin") .upon_receiving("A request for UserA") .with_request("get", "/users/123") .will_respond_with(200, body=Like(expected))
Source code in examples/tests/test_00_consumer.py
test_create_user(pact: Pact, user_consumer: UserConsumer) -> None
¶
Test the POST request for creating a new user.
This test defines the expected interaction for a POST request to create a new user. It sets up the expected request and response from the provider, including the request body and headers, and verifies that the response status code is 200 and the response body matches the expected user data.
Source code in examples/tests/test_00_consumer.py
test_delete_request_to_delete_user(pact: Pact, user_consumer: UserConsumer) -> None
¶
Test the DELETE request for deleting a user.
This test defines the expected interaction for a DELETE request to delete a user. It sets up the expected request and response from the provider, including the request body and headers, and verifies that the response status code is 200 and the response body matches the expected user data.
Source code in examples/tests/test_00_consumer.py
test_get_existing_user(pact: Pact, user_consumer: UserConsumer) -> None
¶
Test request for an existing user.
This test defines the expected request and response from the provider. The provider will be expected to return a response with a status code of 200,
Source code in examples/tests/test_00_consumer.py
test_get_unknown_user(pact: Pact, user_consumer: UserConsumer) -> None
¶
Source code in examples/tests/test_00_consumer.py
user_consumer() -> UserConsumer
¶
Returns an instance of the UserConsumer class.
As we do not want to stand up all of the consumer's dependencies, we direct the consumer to use Pact's mock provider. This allows us to define what requests the consumer will make to the provider, and what responses the provider will return.
The ability for the client to specify the expected response from the provider is critical to Pact's consumer-driven approach as it allows the consumer to declare the minimal response it requires from the provider (even if the provider is returning more data than the consumer needs).