salmon.testing module

A bag of generally useful things when writing unit tests for your Salmon server. The most important thing using the TestConversation vs. RouterConversation to talk to your server.

The TestConversation will use the salmon.server.Relay you have configured to talk to your actual running Salmon server. Since by default Salmon reloads each file you change it will work to run your tests.

However, this isn’t that fast, doesn’t give you coverage analysis, and doesn’t let you test the results. For that you use RouterConversation to do the exact same API (they should be interchangeable) but rather than talk to a running server through the relay, it just runs all the messages through the router directly.

This is faster and will give you code coverage as well as make sure that all the modules (not just your handlers) will get reloaded.

class salmon.testing.RouterConversation(From, Subject)[source]

Bases: salmon.testing.TestConversation

An implementation of TestConversation that routes the messages internally to the Router, rather than connecting with a relay. Use it in tests that are not integration tests.

This creates a set of default values for the conversation so that you can easily send most basic message. Each method lets you override the Subject and Body when you send.

deliver(To, From, Subject, Body)[source]

Overrides TestConversation.deliver to do it internally.

class salmon.testing.TestConversation(relay_to_use, From, Subject)[source]

Bases: object

Used to easily do conversations with an email server such that you send a message and then expect certain responses.

This creates a set of default values for the conversation so that you can easily send most basic message. Each method lets you override the Subject and Body when you send.

begin()[source]

Clears out the queue and Router states so that you have a fresh start.

deliver(To, From, Subject, Body)[source]

Delivers it to the relay.

say(To, Body, expect=None, Subject=None)[source]

Say something to To and expect a reply with a certain address. It returns the message expected or None.

salmon.testing.assert_in_state(module, To, From, state)[source]

Makes sure a user is in a certain state for a certain user. Use these sparingly, since every time you change your handler you’ll have to change up your tests. It’s better to focus on the interaction with your handler and expected outputs.

salmon.testing.clear_queue(queue_dir='run/queue')[source]

Clears the default test queue out, as created by salmon.testing.queue.

salmon.testing.delivered(pattern, to_queue=None)[source]

Checks that a message with that patter is delivered, and then returns it.

It does this by searching through the queue directory and finding anything that matches the pattern regex.

salmon.testing.queue(queue_dir='run/queue')[source]

Creates a queue for you to analyze the results of a send, uses the TEST_QUEUE setting in settings.py if that exists, otherwise defaults to run/queue.

salmon.testing.relay(hostname='127.0.0.1', port=8824)[source]

Wires up a default relay on port 8824 (the default salmon log port).