unittest

In order to provide some convenience a test-case class has been created with the name EMailTestCase deriving from unittest.TestCase, which is meant to be inherited from, as you would do from TestCase:

from mailpit.testing.unittest import EMailTestCase

class MyTest(EMailTestCase):

     def test_send_message(self):
        ...

The class adds a few methods and attributes, so that you are able to assert, if your message has been sent, or if two messages are equal.

Attributes

api_url

If your Mailpit ist not running on localhost:8025, you can set the classattribute api_url to a different URL:

from mailpit.testing.unittest import EMailTestCase

class MyTest(EMailTestCase):

     api_url = "http://my.mailpit.example:8080"

     def test_send_message(self):
        ...

api

Classes inherited from EMailTestCase will connect to the Mailpit-API automatically on creation. They will provide you with the mailpit_api attribute, which is an instance of API:

from mailpit.testing.unittest import EMailTestCase

class MyTest(EMailTestCase):

     def test_send_message(self):
        messages = self.api.get_messages([])
        ...

Assert messages are equal

In order to check, whether two E-Mail messages are equal you can use assertMessageEqual()

from mailpit.testing.unittest import EMailTestCase

class MyTest(EMailTestCase):

    def test_messages_equal(self):
        message1 = self.api.get_message("MessageID1")
        message2 = self.api.get.message("MessageID2")

        self.assertMessageEqual(message1, message2)

Assert message has been received

In order to check, whether an E-Mail message has been received by Mailpit, you can use

:py:meth:~mailpit.testing.unittest.EmailTestCase.assertMessageReceived`

import email
import smtplib
from mailpit.testing.unittest import EMailTestCase

class MyTest(EMailTestCase):

    def test_messages_received(self):
        smtp_server = smtplib.SMTP("localhost", 1025)
        with open(f"tests/mail/email_without_attachment.eml") as fp:
            mail = email.message_from_file(fp)
        smtp_server.send_message(
            mail,
            from_addr="Sender Smith <sender@example.com>",
            to_addrs="Recipient Ross <recipient@example.com>",
        )
        self.assertMessageReceived(
        "20220727034441.7za34h6ljuzfpmj6@localhost.localhost"
    )