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!


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()

*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.


AssertionError – If the messages published don’t match the messages asserted.