Testing Support

The rejected.testing.AsyncTestCase provides a based class for the easy creation of tests for your consumers. The test cases exposes multiple methods to make it easy to setup a consumer and process messages. It is build on top of tornado.testing.AsyncTestCase which extends unittest.TestCase.

To get started, override the rejected.testing.AsyncTestCase.get_consumer() method.

Next, the rejected.testing.AsyncTestCase.get_settings() method can be overridden to define the settings that are passed into the consumer.

Finally, to invoke your Consumer as if it were receiving a message, the process_message() method should be invoked.

Note

Tests are asynchronous, so each test should be decorated with gen_test().

Example

The following example expects that when the message is processed by the consumer, the consumer will raise a MessageException.

from rejected import consumer, testing

import my_package


class ConsumerTestCase(testing.AsyncTestCase):

    def get_consumer(self):
        return my_package.Consumer

    def get_settings(self):
        return {'remote_url': 'http://foo'}

    @testing.gen_test
    def test_consumer_raises_message_exception(self):
        with self.assertRaises(consumer.MessageException):
            yield self.process_message({'foo': 'bar'})
class rejected.testing.AsyncTestCase(methodName='runTest', **kwargs)[source]

TestCase subclass for testing IOLoop-based asynchronous code.

create_message(message, properties=None, exchange='rejected', routing_key='test')[source]

Create a message instance for use with the consumer in testing.

Parameters:
  • message (any) – the body of the message to create
  • properties (dict) – AMQP message properties
  • exchange (str) – The exchange the message should appear to be from
  • routing_key (str) – The message’s routing key
Return type:

rejected.data.Message

get_consumer()[source]

Override to return the consumer class for testing.

Return type:rejected.consumer.Consumer
get_settings()[source]

Override this method to provide settings to the consumer during construction.

Return type:dict
process_message(*args, **kwargs)[source]

Process a message as if it were being delivered by RabbitMQ. When invoked, an AMQP message will be locally created and passed into the consumer. With using the default values for the method, if you pass in a JSON serializable object, the message body will automatically be JSON serialized.

If an exception is not raised, a Measurement instance is returned that will contain all of the measurements collected during the processing of the message.

Parameters:
  • message_body (any) – the body of the message to create
  • content_type (str) – The mime type
  • message_type (str) – identifies the type of message to create
  • properties (dict) – AMQP message properties
  • exchange (str) – The exchange the message should appear to be from
  • routing_key (str) – The message’s routing key
Raises:

rejected.consumer.ConsumerException

Raises:

rejected.consumer.MessageException

Raises:

rejected.consumer.ProcessingException

Return type:

rejected.data.Measurement

rejected.testing.gen_test(func=None, timeout=None)[source]

Testing equivalent of tornado.gen.coroutine(), to be applied to test methods.