mailpit.testing.unittest module

Provides helpers for unittest kind of testing against the Mailpit-API

class mailpit.testing.unittest.EMailTestCase(*args, **kwargs)

Bases: TestCase

unittest.TestCase-derived class with added test-helper methods and attributes, in order to test against the Mailpit-API. Simply derive from this class, as you would from unittest.TestCase and write your tests as you are used to.

e.g.:

class ExampleTestCase(EMailTestCase):

    def testapi_object(self):
        messages: mailpit.client.models.Messages = self.api.get_messages()
        self.assertEqual(0, len(messages.messages))
api_url: str = 'http://localhost:8025'

URL pointing to the Mailpit-API to test, if you need to use another url, override this attribute in your derived class

assertMessageEqual(first: Message, second: Message, msg: str | None = None)

Fail if two instances of Message are not equal as determined by the ‘==’ operator

e.g.:

import mailpit.client.models

class ExampleTestCase(EMailTestCase):

    def test_example(self):
        message1 = mailpit.client.models.Message(...)
        message2 = mailpit.client.models.Message(...)
        self.assertMessageEqual(message1, message2)
assertMessageReceived(message_id: str, msg: str | None = None)

Fail if the passed message has not been sent to Mailpit

mailpit_api: API | None = None

API object created on class setup for testing against Mailpit’s API, access this object if you need to communicate directly with the API in your tests.

classmethod setUpClass() → None

Creates the class attribute object for the API, on creation of this class and classes derived from it. Remember to call super().setUpClass() while subclassing and overriding this method