PyTest email testing
Send and receive email in Pytest Python tests.
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.
How to install Conda on Linux/OSX for python
Conda is a package management system that is popular in the machine learning and python space.
How to test an email address
Test email accounts for testing email addresses in code or online. Create fake email accounts for testing.
How to start selenium in a background process and wait for i...
Spawn Selenium server process before tests start for easier acceptance testing.
Send and receive email in Cypress JS tests with MailSlurp
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
Cypress Email Plugin - Test email accounts in Javascript (an...
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Golang mail Library (SMTP)
How to send and receive emails in Go (test email addresses).
Email APIs for Java and Kotlin projects
Test email sending and receive emails without a mail server.
Java TestNG Selenium user sign up testing with real email ad...
Testing user sign up in Java using TestNG and MailSlurp test email accounts
Codeception PHP acceptance testing using real email address ...
Write acceptance tests in PHP with real email addresses using Codeception and MailSlurp
PHP Email Test Plugins: send and receive email in PHPUnit (e...
How to send and receive emails in PHPUnit tests.
PyTest email testing
Send and receive email in Pytest Python tests.
Selenium test email plugins: send and receive email in tests...
Receive emails in Java test suites using MailSlurp, Junit, and Selenium.
Receive email in PHP: using MailSlurp to send and receive em...
Test email in PHP using real email addresses
Robotframework Python Testing using real email addresses
Python automation email testing Robotframework plugin
Testing authentication using real email addresses in Ruby wi...
Cucumber example project using Capybara to test user authentication using real email addresses.
Test applications with real emails using Serenity BDD, JBeha...
Email acceptance testing with Serenity and MailSlurp. Test applications with real email addresses.
.NET SpecFlow testing using MailSlurp email accounts and Sel...
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Test email accounts with Jest and Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
Test Emails in Selenium with DotNet, CSharp and MailSlurp
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
Test emails with Cucumber and Ruby
Generate test email accounts with Ruby and Cucumber. Test email sign-up, password verification and more.
Test user sign-up with NodeJS and WebDriver
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email v...
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
How To Test Emails Before You Send
There are many free tools to test emails before sending. This can help prevent spam warnings and increase deliverability...
Testing OTP password link username and password for 2 factor...
Testing OTP password link username and password for 2 factor authentication (2FA)
Test email address
Free test email address for testing emails online with web dashboard or REST API.
Testing email-related functionality with MailSlurp
Use MailSlurp to test email related functionality using real email addresses.
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Testing Webhooks
How to test HTTP webhooks using MailSlurp test hooks.
Send SMTP email with Python (using SMTPLib)
How to send emails with Python's built-in SMTP client and MailSlurp mail servers.
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS