Test 02 Message Consumer¶
Test Message Pact consumer.
Pact was originally designed for HTTP interactions involving a request and a response. Message Pact is an addition to Pact that allows for testing of non-HTTP interactions, such as message queues. This example demonstrates how to use Message Pact to test whether a consumer can handle the messages it. Due to the large number of possible transports, Message Pact does not provide a mock provider and the tests only verifies the messages.
A note on terminology, the consumer for Message Pact is the system that receives the message, and the provider is the system that sends the message. Pact is still consumer-driven, and the consumer defines the expected messages it will receive from the provider. When the provider is being verified, Pact ensures that the provider sends the expected messages.
In this example, Pact simply ensures that the consumer is capable of processing
the message. The consumer need not send back a message, and any sideffects of
the message must be verified separately (such as through assert
statements or
as part of the usual unit testing suite).
There is currently a bug whereby the
given
andexpects_to_receive
have swapped meanings compared to the reference implementation. This will be addressed in a future release.
A good resource for understanding the message pact testing can be found in the Pact documentation.
Attributes¶
log = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions¶
handler() -> Handler
¶
Fixture for the Handler.
This fixture mocks the filesystem calls in the handler, so that we can verify that the handler is calling the filesystem correctly.
Source code in examples/tests/test_02_message_consumer.py
pact(broker: URL, pact_dir: Path) -> Generator[MessagePact, Any, None]
¶
Set up Message Pact Consumer.
This fixtures sets up the Message Pact consumer and the pact it has with a provider. The consumer defines the expected messages it will receive from the provider, and the Python test suite verifies that the correct actions are taken.
For each interaction, the consumer defines the following:
(
pact.given("a request to write test.txt")
.expects_to_receive("empty filesystem")
.with_content(msg)
.with_metadata({"Content-Type": "application/json"})
)
NOTE: There is currently a bug whereby the `given` and `expects_to_receive`
have swapped meanings. This will be addressed in a future release.
Source code in examples/tests/test_02_message_consumer.py
test_read_file(pact: MessagePact, handler: Handler) -> None
¶
Source code in examples/tests/test_02_message_consumer.py
test_write_file(pact: MessagePact, handler: Handler) -> None
¶
Test write file.
This test will be run against the mock provider. The mock provider will expect to receive a request to write a file, and will respond with a 200 status code.