PyTest email testing

Send and receive email in Pytest Python tests.

test emails in python

Send and receive emails in Python from MailSlurp test email accounts.

Add dependencies

To use test email accounts in PyTests first sign up for a free MailSlurp account.

Then install the MailSlurp python library and PyTest with pip:

pip install mailslurp-client pytest

Or add them to your requirements.txt:

mailslurp-client==7.0.9
pytest==4.3.1

Configure email client

Next add the MailSlurp client and configure it with your MailSlurp API Key.

import mailslurp_client

configuration = mailslurp_client.Configuration()
configuration.api_key['x-api-key'] = 'YOUR_MAILSLURP_API_KEY'

Common usage

Create test email accounts

We can use MailSlurp to create test inboxes. These have real email addresses.

def test_can_create_email_address():
    with mailslurp_client.ApiClient(configuration) as api_client:
        # create an inbox using the inbox controller
        api_instance = mailslurp_client.InboxControllerApi(api_client)
        inbox = api_instance.create_inbox()

        # check the id and email_address of the inbox
        assert len(inbox.id) > 0
        assert "mailslurp.com" in inbox.email_address

Send emails

You can send emails in Python using MailSlurp too:

def test_can_send_email():
    with mailslurp_client.ApiClient(configuration) as api_client:
        # first create an inbox
        api_instance = mailslurp_client.InboxControllerApi(api_client)
        inbox = api_instance.create_inbox()

        # send email from the inbox
        send_email_options = mailslurp_client.SendEmailOptions()
        send_email_options.to = [inbox.email_address]
        send_email_options.subject = "Hello"
        send_email_options.body = """
        <h1>MailSlurp supports HTML</h1>
        """
        send_email_options.is_html = True
        api_instance.send_email(inbox.id, send_email_options=send_email_options)

Receive emails and extract contents

To receive emails in PyTest use MailSlurp's waitFor controllers to receive emails directly in code. This test demonstrates how to send an email then receive its content. In this way we can read emails in Python tests.

def test_can_receive_emails_and_extract_content():
    with mailslurp_client.ApiClient(configuration) as api_client:
        # create two inboxes for testing
        inbox_controller = mailslurp_client.InboxControllerApi(api_client)
        inbox_1 = inbox_controller.create_inbox()
        inbox_2 = inbox_controller.create_inbox()

        # send email from inbox 1 to inbox 2
        send_email_options = mailslurp_client.SendEmailOptions()
        send_email_options.to = [inbox_2.email_address]
        send_email_options.subject = "Hello inbox 2"
        send_email_options.body = "Your code is: 123"
        inbox_controller.send_email(inbox_1.id, send_email_options=send_email_options)

        # receive email for inbox 2
        waitfor_controller = mailslurp_client.WaitForControllerApi(api_client)
        email = waitfor_controller.wait_for_latest_email(inbox_id=inbox_2.id, timeout=30000, unread_only=True)

        assert email.subject == "Hello inbox 2"

        # extract content from body
        pattern = re.compile('Your code is: ([0-9]{3})')
        matches = pattern.match(email.body)
        code = matches.group(1)

        assert code == "123"

Next steps

MailSlurp is free for personal use. So check out www.mailslurp.com for more.

Related content

Golang email library

Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.

Python email API - send and receive email in code and tests

Send and receive email in Python, create test email accounts and more with the official PyPI MailSlurp package.

MailSlurp Zapier Email Plugin

Create email accounts to send and receive emails and attachments as Zapier actions and triggers.

Email for testing

Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.

How to wait for Selenium to start during Codeception tests

Example tutorial for how to wait until webdriver and Selenium have started during Codeception PHP tests

Email API for email marketing and more

APIs for email marketing and social campaign testing. Send, receive, validate and test emails in code and online.

Ready to dive in?Start building email applications today.