pytest Email Verification Test: Codes, Links and Resets in Python
To test email verification in pytest, give every test its own temporary inbox, let your app send the email there, and read the code from it in the same test. The tempmailo library for Python covers the inbox side with three calls: create, wait_for_message and delete. Below are the fixtures, four tests you can copy (code, link, password reset, sender) and the CI setup.
The addresses are ordinary temp mail addresses of TempMailo, made through the API instead of the website. They receive mail and can't send it, which is all a sign-up test needs.
What you need
- Python 3.10 or newer. The library itself has no dependencies; the examples also use
requeststo call your app. - A free TempMailo account and an API key. Sign up, confirm the email, then create a key on the API page of your account.
- An app that sends its emails to any address a test gives it: a staging server, a preview deploy or a local build with real mail sending.
The free limits are on the API page: 100 new addresses a day, 60 requests a minute, and each address lives 10 or 60 minutes. With one fresh address per test, that is up to 100 email tests a day.
Install the library and set the key
pip install tempmailo pytest requests
export TEMPMAILO_API_KEY=tmk_your_key_here
The client reads TEMPMAILO_API_KEY by itself. Keep the key in an environment variable or a CI secret, never in the repository.
Fixtures: a new inbox for every test
Put this in tests/conftest.py. The client lives for the whole test session. Every test that asks for inbox gets a new address, and the address is deleted when the test ends, whether it passed or failed.
import os
import pytest
from tempmailo import RateLimitError, TempMailo
@pytest.fixture(scope="session")
def mail():
if not os.environ.get("TEMPMAILO_API_KEY"):
pytest.skip("set TEMPMAILO_API_KEY to run the email tests")
return TempMailo()
@pytest.fixture
def inbox(mail):
try:
box = mail.create(lifetime=600, client="pytest-email 1.0")
except RateLimitError as error:
pytest.skip(f"TempMailo limit reached, try again in {error.retry_after} s")
yield box
mail.delete(box.token)
Without a key the email tests are skipped, not failed, so anyone without a key can still run the rest of the suite. If a limit is used up when a test asks for a new address, that test is skipped too, and the message says how long to wait.
Test 1: read the confirmation code
import html
import os
import re
import requests
APP = os.environ.get("APP_URL", "https://staging.your-app.example")
def sign_up(email):
r = requests.post(f"{APP}/api/signup", json={"email": email, "password": "a-long-test-password"})
assert r.status_code == 201
def test_signup_code(mail, inbox):
sign_up(inbox.email)
letter = mail.wait_for_message(inbox.token, timeout=120, match=lambda m: "code" in m.subject.lower())
code = re.search(r"\b\d{6}\b", letter.plain or letter.html).group()
r = requests.post(f"{APP}/api/confirm", json={"email": inbox.email, "code": code})
assert r.status_code == 200
Swap the two endpoints for your app's sign-up and confirm calls. If sign-up is a web form, drive it with Playwright or Selenium and keep the email part as it is.
wait_for_message asks for new mail every 5 seconds, skips messages that don't pass match and returns the whole message: subject, sender, plain text, HTML and attachments. The pattern \b\d{6}\b finds a six-digit code. If your codes look like "418 507" or contain letters, change the pattern to match. The letter.plain or letter.html part covers emails that have no plain-text version.
Test 2: open the confirmation link
def test_confirmation_link(mail, inbox):
sign_up(inbox.email)
letter = mail.wait_for_message(inbox.token, timeout=120)
href = re.search(r'href="([^"]*/confirm\?[^"]*)"', letter.html).group(1)
link = html.unescape(href) # & in the HTML becomes &
assert link.startswith(APP)
assert requests.get(link).status_code == 200
In HTML, the & between link parameters is written as &, so unescape the link before you open it. The startswith check catches a bug that is easy to miss by hand: a staging email that points to production.
Test 3: password reset in the same inbox
def test_password_reset(mail, inbox):
sign_up(inbox.email)
first = mail.wait_for_message(inbox.token, timeout=120)
requests.post(f"{APP}/api/password-reset", json={"email": inbox.email})
reset = mail.wait_for_message(inbox.token, timeout=120, after=first.id)
assert "reset" in reset.subject.lower()
assert "/reset?token=" in reset.plain
after=first.id makes the second wait skip the sign-up email, so the test can't pass on the wrong message.
Test 4: check the sender and the subject
def test_sender(mail, inbox):
sign_up(inbox.email)
letter = mail.wait_for_message(inbox.token, timeout=120)
assert letter.sender == "no-reply@your-app.example"
assert letter.subject == "Your confirmation code"
For headers such as DKIM-Signature or List-Unsubscribe, get the raw message with mail.source(inbox.token, letter.id) and parse it with email.message_from_string from the standard library.
Run the email tests in CI
Add the key as a repository secret named TEMPMAILO_API_KEY, then run the tests like any others. A GitHub Actions example:
# .github/workflows/email-tests.yml
name: email-tests
on: [push, pull_request]
jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.15"
- run: pip install tempmailo pytest pytest-xdist requests
- run: pytest tests -n 4
env:
TEMPMAILO_API_KEY: ${{ secrets.TEMPMAILO_API_KEY }}
APP_URL: https://staging.your-app.example
Parallel runs with pytest-xdist work, because each worker creates its own inboxes. A test that waits for mail makes about 12 requests a minute (one every 5 seconds), so up to four tests can wait at the same time and stay within the limit of 60 requests a minute. When a request hits that limit and the API asks to wait 60 seconds or less, the client waits and retries by itself.
Why a test fails, and what to check
- TimeoutError after 120 seconds. No matching email arrived. Look at your app's mail log, check that
matchfits the real subject, and make sure the email went toinbox.emailand not to a fixed test address. - RateLimitError. A limit is used up.
error.scopesays which one,error.retry_afterhow many seconds to wait. - ExpiredError. The address has lived its 10 or 60 minutes. Create inboxes inside tests, not once for a long session.
- AuthError. The key is wrong or was deleted in your account.
When a real mailbox is the better choice
TempMailo addresses only receive. A test that replies to an email, checks whether a message lands in spam at Gmail or Outlook, or keeps an account alive for weeks needs a mailbox you own. The same goes for anything where the address itself has to stay valid after the test, such as a shared QA account. To check a sign-up email by hand, without any code, see how to test sign-up emails.
FAQ
Does it work without pytest?
Yes. The client is plain Python, so unittest, behave or a simple script work the same way. The tempmailo-python repository on GitHub has a script version in examples/signup_test.py.
Is there an async version?
The client is synchronous. In asyncio code, run the wait in a thread: await asyncio.to_thread(mail.wait_for_message, inbox.token, timeout=120).
Can I choose the address?
Yes: mail.create(lifetime=600, login="signup-test-01"). mail.domains() returns the domains you may pass as domain and the rules for the login.
Can tests get mail without polling?
Yes, with a webhook: TempMailo sends every new email to your HTTPS endpoint as a signed request, and verify_webhook checks the signature. For most test suites polling is simpler, since a test runner usually has no public URL.
Is there a JavaScript version?
Yes, tempmailo-client on npm, with the same methods. The Playwright guide shows it in a sign-up test.
Sources
- tempmailo on PyPI and its source on GitHub (MIT license)
- TempMailo API reference: methods, limits, errors and webhooks
- The API guide with curl examples, for other languages


