Testing¶
Once you’ve written code to publish or consume messages, you’ll probably want
to test it. The fedora_messaging.testing
module has utilities for common
test patterns.
If you find yourself implementing a pattern over and over in your test code, consider contributing it here!
- fedora_messaging.testing.mock_sends(*expected_messages)[source]¶
Assert a block of code results in the provided messages being sent without actually sending them.
This is intended for unit tests. The call to publish is mocked out and messages are captured and checked at the end of the
with
.For example:
>>> from fedora_messaging import api, testing >>> def publishes(): ... api.publish(api.Message(body={"Hello": "world"})) ... >>> with testing.mock_sends(api.Message, api.Message(body={"Hello": "world"})): ... publishes() ... publishes() ... >>> with testing.mock_sends(api.Message(body={"Goodbye": "everybody"})): ... publishes() ... AssertionError
- Parameters:
*expected_messages – The messages you expect to be sent. These can be classes instances of classes derived from
fedora_messaging.message.Message
. If the class is provided, the message is checked to make sure it is an instance of that class and that it passes schema validation. If an instance is provided, it is checked for equality with the sent message.- Raises:
AssertionError – If the messages published don’t match the messages asserted.